Reputation: 465
I'm trying to create a jar file for one of my Java files. However, once I create it and try to run it, I get a java.lang.NoClassDefFoundError.
I did the following to create the jar. The file is Worker.java and it's in src/beatDB.
jar cfm Worker.jar Manifest.txt src/beatDB
My Manifest.txt file is just
Main-Class: Main-Class: beatDB.Worker \n
(with a new line afterwords)
When I run java -jar Worker.jar
, I get this error.
Exception in thread "main" java.lang.NoClassDefFoundError:
beatDB/Worker
Caused by: java.lang.ClassNotFoundException: beatDB.Worker
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)
However if I run java beatDB/Worker
, the file runs fine.
Does anyone know how to fix this problem?
Upvotes: 0
Views: 2481
Reputation: 11209
You should jar the manifest along with the .class files, not the .java files.
.java files contain the source code.
.class files contain the compiled code. For Java it is called byte code.
You want the package structure of your classes to be reflected in your jar file. If you have an Animal class under com.example, then your jar file should contain com/example/Animal.class
See How to create jar file with package structure? for the proper jar file package structure.
Upvotes: 1