0

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

4
  • Did you try replacing ";" by ":" in your classpaths? Commented Nov 28, 2012 at 0:30
  • @reprogrammer tried with ":" but no help.. Commented Nov 28, 2012 at 0:33
  • 1
    So, update the question to use : as the classpath separator. Because, that's one obvious reason for ClassNotFoundException. Commented Nov 28, 2012 at 0:35
  • @reprogrammer ok sir I will do Commented Nov 28, 2012 at 1:11

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion, I have removed the stars and put only the base folders but now get a new ClassNotFoundException thrown on org/apache/log4j/Logger
Then you now no longer got the .jar files in the library folder. Try listing them explicitely in the classpath, or try keeping the * for the folders where you do want all contents to be separate class path entries.
You're a star!(no pun intended) No really thanks so much that did it. I had to modify the command prompt quite a bit from the original one so I have updated my question with the answer at the end so people can see what works in this case
3

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

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.