Noclassdeffounderror While Trying to Run My Jar with Java.Exe -Jar...What's Wrong

How can I solve java.lang.NoClassDefFoundError?

After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader (in this case java.net.URLClassLoader), which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.

Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.

For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.

Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDEs like Eclipse and IntelliJ IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.

Application (.jar) error - NoClassDefFoundError

You should create a single main jar file that will contain all needed jars (libraries) and then you can use Launch4j to generate your exe file.

Take a look here Launch4J - how to attach dependent jars to generated exe

The dreaded java.lang.NoClassDefFoundError

A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.

In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.

The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:

java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass

To save the effort of typing and editing the argument in commandline everytime, use a .bat file.

Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.

java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass

ClassNotFoundException when running executable jar

If the not found class is inside a jar, you have to provide the classpath arguments; for example:

java -cp a.jar -jar supervisor.jar


Related Topics



Leave a reply



Submit