Rohan Honwade
Rohan Honwade

Reputation: 61

Classpath error - Unable to find main class and log4j

I am trying to run a jar using this command in my shell script -

java -Dlog4j.configuration=path/to/log4j.properties -classpath path/to/log4j.jar:path/to/another.jar -cp my/jarfile/to/run/myjar.jar com.xyz.TestSuiteRunner CREATE_4_SL

But when I run this, I get the error as shown -

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

at com.xyz.TestSuiteRunner.<clinit>(TestSuiteRunner.java:27) 
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    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)
    ... 1 more
Could not find the main class: com.xyz.TestSuiteRunner.  Program will exit.

After referring to other similar posts, I do know that I can create a runnable jar file using a manifest file but I don't want to do it that way. Can anyone please let me know where am I going wrong over here?

Upvotes: 0

Views: 2884

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

You've got both -classpath and -cp switches; -cp is just an abbreviation for -classpath, and you can't have more than one of these. The last one is the one that "sticks", so any classes named in the first -classpath switch won't be found. You need to combine those arguments into one long path; i.e.,

-classpath path/to/log4j.jar:path/to/another.jar:my/jarfile/to/run/myjar.jar

The error about not being able to find the main class is a bit of a red herring; the class is clearly found, it just can't be initialized because its dependencies aren't found.

Upvotes: 2

Related Questions