Search code examples
javaclassnotfoundexception

Compilation error with external Jar


I am trying to command line compile my code and I cannot get it to run properly. My project was in eclipse and I could specify the location of a jar file to include but the command line is another issue. All .java and the .jar file are in the same director.

I can compile with

javac -classpath opencsv-2.3.jar *.java

I get the following exception if I try to run my code.

java ClassificationGenerator

Exception in thread "main" java.lang.NoClassDefFoundError: au/com/bytecode/opencsv/CSVReader

I tried running the code with

java -classpath opencsv-2.3.jar ClassificationGenerator

but then I get

Exception in thread "main" java.lang.NoClassDefFoundError: ClassificationGenerator
Caused by: java.lang.ClassNotFoundException: ClassificationGenerator
    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)

Solution

  • You need to give the jar in the classpath while running, aw well:

    java -classpath .;opencsv-2.3.jar ClassificationGenerator