aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification on classpaths Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification on classpaths" Watch "Clarification on classpaths" New topic
Author

Clarification on classpaths

richard rehl
Ranch Hand

Joined: May 21, 2007
Posts: 36
Just want to make sure I understand this. From experimenting with an example in Chapter 10, if you have a class file:
which is saved in the directory:
MyProject/source/com/wickedlysmart/MyClass.java
and compiled into the directory:
MyProject/classes/com/wickedlysmart/MyClass.class
Invoking java on MyClass.class won't work unless
A. you use the fully qualified name com/wickedlysmart/MyClass
B. you are in a directory at least one level above the package root (com), in this case, at least MyProject/classes.
C. Also, if you're currently in another subdirectory defined off the project root, in this case, MyProject/source, you can't say
java ../classes/com/wickedlysmart/MyClass without getting a compiler error.

edit: invoking
java -classpath ../classes com/wickedlysmart/MyClass
DOES work... so figured that one out

And, although SB say that adding a dot to the classpath will cause java to search the current directory, I find that if I'm currently in the directory:
MyProject/classes/com/wickedlysmart
invoking either "java -classpath . MyClass" or "java -classpath . com/wickedlysmart/MyClass" causes a NoClassDefFoundError, which seems to contradict what they say, which leads me to inference B above.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
If you put your class into a package then it should be refered with fully qualified name.

So in your case your class name is always "com.wickedlysmart.MyClass".

The confustion about the current directory:

Let say you stored this class under the folder:

build
|------com
|----wickedlysmart
|---MyClass.class

Now let assume you override the classpath variable at the System level and it says Classpath=ojdbc14.jar

Now if you run the class from the build folder : java com.wickedlysmart.MyClass

Java wll not search for the class file in the current folder(By default java or javac will look into the current folder if the classpath is not overriden anywhere either at system level or at command level), it will search only in the classpath given.

So you have to tell java to include current folder in the classpath too

java -cp . com.wickedlysmart.MyClass

If you go to the wickedlysmart folder and execute java MyClass

Java will fail to find your class because there is no such a class called MyClass,being put in a package now your classname is com.wickedlysmart.MyClass

This link gives more details : webpage


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
 
Consider Paul's rocket mass heater.
 
subject: Clarification on classpaths