Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why adding class path caused main class not found?

Tags:

java

I get a NoClassDefFoundError when trying to run a Java class without providing the proper class path. However when added the needed class path, java complaints it cannot find the main method. If you have any idea of what's happening here, please point me in the right direction. Thank you

$ java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix1D
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
    at java.lang.Class.getMethod0(Class.java:2856)
    ... 6 more

$ java -cp resources/colt.jar MyClass
Error: Could not find or load main class MyClass
like image 653
cody Avatar asked Feb 02 '26 19:02

cody


1 Answers

Assuming MyClass isn't in a package, you need something like (on Windows)

java -cp resources/colt.jar;. MyClass

or (otherwise)

java -cp resources/colt.jar:. MyClass

To also include the current directory. Alternatively, you can set the CLASSPATH environment variable.

On Windows,

set "CLASSPATH=resources/colt.jar;."

otherwise something like (depending on your shell)

export CLASSPATH="resources/colt.jar:."

then

java MyClass
like image 51
Elliott Frisch Avatar answered Feb 05 '26 08:02

Elliott Frisch