Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class not found

I'm trying to run a java program from a jar file. Java can't find a supporting marc4j class. What am I doing wrong. Here are the details

Within my current directly is MarcTry.jar which has my main class. There is also marc4j.jar which has the missing class:

org/marc4j/MarcReader

For example:

java -jar MarcTry.jar Exception in thread "main" java.lang.NoClassDefFoundError: org/marc4j/MarcReader Caused by: java.lang.ClassNotFoundException: org.marc4j.MarcReader 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:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: marctry.Main. Program will exit.

I've tried

java -jar MarcTry.jar -classpath marc4j.jar

with and without the marc4j.jar a fully qualified path.

Any ideas are welcome.

like image 618
Thwaites Avatar asked May 12 '11 19:05

Thwaites


Video Answer


1 Answers

The classpath is ignored when you are using the "-jar" switch. Specify both jars with "-classpath" and execute the main class with the fully qualified name.

E.g. java -cp MarcTry.jar;marc4j.jar com.domain.MainClass

.. or add marc4j.jar to the classpath entry in the manifest file of MarcTry.jar

You can read about adding jars to the classpath of the manifest file here:Adding Classes to the JAR File's Classpath

like image 148
Kaj Avatar answered Oct 05 '22 10:10

Kaj