Error while running jar file on the system
I have a java file, for example:
import org.xBaseJ.DBF;
import org.xBaseJ.fields.CharField;
import org.xBaseJ.fields.NumField;
import org.apache.log4j.*;
public class Example2 {
public static void main(String args[]){
..........
}
}
I created this for an Example2.jar file to run by following these steps:
1) javac Example2.java
2) java Example2
3) This will produce a .class file needed for the JAR file.
4) Next create a manifest file (saved using the extension .txt) using the text editor and input the following
Main-Class: Example2
or whatever your file name is.
5) Next create the JAR file using this code:
jar cfe Example2.jar Example2 Example2.class
After step 5, I got a jar file named "Example2.jar". I tried to run the jar file using the following command:
java -jar HelloWorld.jar
But I am getting the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xBaseJ/DBF at Example2.main(Example2.java:14) Caused by: java.lang.ClassNotFoundException: org.xBaseJ.DBF at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
I donβt understand what is the reason? Please guide me?
source to share
The good thing is that when you create a Jar, just look at this manifest first, which might help to include External Libraries in it.
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Whereas creating and providing chages for manifestation just need to run this class with configuration
http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html
to see both links.
just see this example.
run java and get class file
javac Test.java
and if u works with some other external libraries then do the following.
javac -classpath xxx.jar Test.java
and look at the manifest config and make this file with such external changes.
menifest.txt
Main-Class: Test
Class-Path: xxx.jar xxxx.jar
then you need to make the jar file like this. run this command
jar cfm jarName.jar manifest.txt Test.class
and hence we made u may have a jarfile in the same path.
source to share
The error you are seeing is caused by an incorrect classpath . I am assuming that when you compiled the class, you provided the classpath somehow (either by passing the "-classpath" argument, or by setting the CLASSPATH environment variable). The problem is that the compilation class path is separate from the execution path path. Thus, you just need to make sure that all dependencies (other jar files most likely) that were in the classpath when the class was compiled are also added to the classpath when the jar is run. For a jar file, this is usually done by adding "Class-Path" to the manifest.
An alternative method would be to specify the classpath using the arg command line or an environment variable and include your Example2.jar file in that classpath and run java Example2
(without "-jar").
source to share