This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packages - is this a reasonable way to deal with them?

 
Rupert Brown
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've alway struggled with packages, but think that I've finally figured it out.

On a Windows machine, the package name has to match up to a directory structure. So if you want to put things in a package called suncertify.db, that relates to a directory structure called suncertify\db. But to make all this work, Java has to know where suncertify is, and that is where the CLASSPATH variable comes in.

Assume I want to create the files in my suncertify.db package in a complete directory path called:

C:\Rupert\myJavaFiles\suncertify\db

And I have some test files in another directory:

C:\Rupert\myJavaFiles\test\db

In my Java files I include the statement package suncertify.db;and in my test files package test.db;

I include in my CLASSPATH the following C:\Rupert\myJavaFiles so that the whole CLASSPATH thing, when you type set classpath into a prompt looks like

CLASSPATH=.;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip;C:\Rupert\myJavaFiles

Then I can happily compile my files doing something like this:

C:\Rupert\myJavaFiles>javac suncertify\db\Contractor.java
C:\Rupert\myJavaFiles>javac test\db\ContractorTest01.java


But the real wrinkle happens with this:

C:\Rupert\myJavaFiles>java test\db\ContractorTest01
Exception in thread "main" java.lang.NoClassDefFoundError: test\db\ContractorTest01 (wrong name: test/db/ContractorTest01) ...


Even though it's a Windows machine you have to type the command:

C:\Rupert\myJavaFiles>java test/db/ContractorTest01

(slashes the other way) Have I got that right? Is that a reasonable way of doing it?
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though it's a Windows machine you have to type the command:

C:\Rupert\myJavaFiles>java test/db/ContractorTest01

(slashes the other way) Have I got that right? Is that a reasonable way of doing it?


The java program takes the fully qualified class name as the parameter. So, it is...

C:\Rupert\myJavaFiles>java test.db.ContractorTest01


Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic