views:

146

answers:

3

I try to figure out how organize source and class files working with packages. I found a very useful tutorial. But I still have some questions.

As far as I understood it is a good practice to have an isomorphism between name of packages and name of the directories where elements of a package are stored. For example if I have a package named "aaa.bbb.ccc" which contains class "ddd" it is a good practice to have a class file called "ddd.class" and located in "$CLASSPATH/aaa/bbb/ccc/". Did I get it right?

If it is the case, will Java compiler put *.class file into a correct directory automatically?

At least I was not able to get this behavior. I set $CLASSPATH variable to be equal to "/home/myname/java/classes". I executed "javac KeyEventDemo.java" which contain "package events;". As a result I expected that javac will create a subdirectory "events" in the "/home/myname/java/classes" and put KeyEventDemo.class file to this subdirectory. It did not happened. I tried to help to javac and created "events" subdirectory by myself. I used "javac" again but it does not want to put class files to the "/home/myname/java/classes/events". What am I doing wrong?

+1  A: 

It is not only good practice but a must in most cases.

consider a Java class named:

com.example.Hello

If you store it on the filesystem, it has to got to

/path/to/my/classes/com/example/Hello.java

The compiler (or at least the vast majority) will create the class file at

/path/to/my/classes/com/example/Hello.class

Personally I would not use the CLASSPATH variable to set the path but the -cp option on java. A call to the above "application" could be done with:

java -cp /path/to/my/classes com.example.Hello
Andreas_D
+3  A: 

You need to use the -d option to specify where you want the .class files to end up. Just specify the base directory; javac will create any directories necessary to correspond to the right package.

Example (based on your question):

javac -d ~/java/classes KeyEventDemo.java
Chris Jester-Young
And to clarify $CLASSPATH: it will tell the JVM where to look for classes during runtime, not to tell javac where to create classes this answer and your experience show.
Petri Pellinen
+3  A: 

For example if I have a package named "aaa.bbb.ccc" which contains class "ddd" it is a good practice to have a class file called "ddd.class" and located in "$CLASSPATH/aaa/bbb/ccc/". Did I get it right?

That's not "good practice" - this is how the Sun JDK expects things to be. Otherwise, it will not work. Theoretically, other Java implementations could work differently, but I don't know any that do.

If it is the case, will Java compiler put *.class file into a correct directory automatically?

Yes

What am I doing wrong?

The source code must also already follow this structure, i.e. KeyEventDemo.java must reside in a subdirectory named "events". Then you do "javac events/KeyEventDemo.java", and it should work.

Michael Borgwardt
If you use the `-d` option (highly recommended, so binaries live in a different place from the source), then in theory, your source does not have to follow that hierarchy.
Chris Jester-Young
On the java.sun.com I found out that it can be convenient to keep *java and *class files in different directories. And I would like to use this approach.
Roman
As Chris wrote, you can use the -d option for this. But it's still a very good idea to keep the source files in the same directory hierarchy, or it will be very confusing.
Michael Borgwardt