ClassNotFoundException despite class in the classpath

五迷三道 提交于 2019-12-12 05:08:15

问题


The ClassNotFoundException questions are found by the dozens on SO but even so I did not find the answer to my problem in past answers :( Basically I get a ClassNotFoundException while trying to run a large open source program from the command line using a command prompt that is provided in the project's doc. The prompt is as follow:

java -cp "target/classes/*;../../Algotrader/code/target/classes/*;../../lib/*;../../target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams

(note: the original command actually says java.exe but I changed it to java as java.exe is not recognised as a command on my Mac)

The exception is thrown for the SimulationStarter class as shown by the stack trace:

Exception in thread "main" java.lang.NoClassDefFoundError: com/algoTrader/starter/SimulationStarter
Caused by: java.lang.ClassNotFoundException: com.algoTrader.starter.SimulationStarter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

From Eclipse I can see that SimulationStarter.class is in AlgoTrader/code/target/classes/com/algoTrader/starter, which looks in line with the path provided in the command prompt.

So my question is: what could be the cause(s) for this exception other than the class being incorrectly placed in the classpath?

Also not sure this makes any difference but the project is kept under svn and Maven and I am running it on a Mac.

CORRECT CLASSPATH

In the end the classpath given in the command prompt was at fault. The correct paths (at least ones that solve the problem) are:

java -cp "target/classes/:../../Algotrader/code/target/classes/:target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams

The main differences with the original prompt is the removal of the stars except for the folder that contains the jar files and the shortening of the base classpath paths to capture only the base directories. Also ":" should be used instead of ";" on Macs and Linux as per @reprogrammer answer


回答1:


Are you sure you need all the * there?

Usually, you will want to give the base directories of the classpath, not subfolders.

E.g. when building your application into "bin", you would use java -cp bin mainclass, not java -cp bin/*! The support for * is generally a bit flaky, as it is a shell metacharacter, and you need to get quoting right. It can really screw you if you have an incorrect classpath. I've seen people have issues because they added README.TXT to their classpath.




回答2:


The classpath syntax is OS-dependent. The classpath separator in Linux and Mac OS X is : not ;.



来源:https://stackoverflow.com/questions/13595718/classnotfoundexception-despite-class-in-the-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!