4

I'm trying 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 files into the correct directory automatically?

I was not able to get this behavior. I set the $CLASSPATH variable to "/home/myname/java/classes". I executed javac KeyEventDemo.java which contains package events;. I expected that javac will create a subdirectory events under /home/myname/java/classes and put the KeyEventDemo.class in this subdirectory.

It did not happen. 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 under "/home/myname/java/classes/events". What am I doing wrong?

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

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.

3 Comments

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.
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.
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.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.