views:

115

answers:

2

Hi, I am trying to run the java prog I've built, but I want to run it from a specific directory. When specifying the relative path to the class file I want to run, that path contains a directory with a period in it, and it seems to be tripping java up; So for example I try to run:

java -classpath myPath/myPath-1.2.3/myLongPath myPath/myPath-1.2.3/myLongPath/myProg

Java errors out saying that it cannot find the class (NoClassDefFoundError); This makes sense because I see that java is looking in different directory than the one I specified; It is looking in: myPath/myPath-1/2/3/myLongPath instead of: myPath/myPath-1.2.3/myLongPath

Try as I might, I cannot figure out how to specify to java.exe that the directory I want it to look in contains periods. I tried \ escaping the periods, but that doesn't help. Anyone run into this problem before? btw, I am running on linux within gnome terminal. Thanks for any help.

+5  A: 

The final parameter in the call to java is the name of the class to run. This is not a file name, but a class name. It includes the full package name (unless the class is in the default package), separated by dots (not slashes). Neither the classname nor any package name can include dots. The folder that represent the path to the package must not be included in the directories included in the classpath (only the top directory for the class folder should be there).

In your case, that seems to be just myProg, but to make sure, what is the class name (including package name) of the class with the main method?

Example:

If I have a class mypackage.mysubpackage.MainClass, and the class file is in /home/me/project/1.3/build/mypackage/mysubpackage/MainClass.class, then the command to run the class would be java -cp /home/me/project/1.3/build mypackage.mysubpackage.MainClass.

Thilo
Thanks, the example you gave helped me figure out what I was doing wrong. So the command should have been: java -classpath myPath/myPath-1.2.3/myLongPath myProgNow I see that the package path never has periods in it, which is why I should never encounter the period-in-package-path problem so long as I am specifying my package path correctly.
lomilomi26
+1  A: 

Java uses the period as the package component separator, so it simply cannot appear in class names. Since class names are tied to directory structures, it cannot appear in directory names used in class paths either, and no amount of escaping will help you there. (It would have been better to use the directory separator itself as the package component separator, but they differ between operating systems, and Java wanted to be OS independent. This is one of the prices to be paid for that.)

Kilian Foth
The dot can appear in the classpath, no problem there. It just can't exist in a package name, i.e. it must not be in a directory *inside* a classpath location.
Joachim Sauer