开发者

running with Java.exe when period exists in directory name

开发者 https://www.devze.com 2022-12-20 18:06 出处:网络
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 p开发者_如何学JAVAath contains a di

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 p开发者_如何学JAVAath 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消