6

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.

Thwaites
  • 63
  • 1
  • 4

2 Answers2

5

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

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • i think that solved my problem Thanks! Now a different class is missing, but I know how to fix that next. (The semicolon in your answer is a typo. A colon worked.) – Thwaites May 12 '11 at 19:32
  • colon vs semicolon is platform specific – Jeff Storey May 12 '11 at 19:34
  • Great! You can, if you want, mark this answer as correct. That will marks to others that the question has been answered. – Kaj May 12 '11 at 19:35
  • ; is for Windows and : is for Linux. – Bhushan May 12 '11 at 19:38
  • @Jeff Storey and @Bhushan.Yes I know that, and I guessed that the OP also understood that he needed to change that if he was on *nix. – Kaj May 12 '11 at 19:42
  • @Kaj, that comment from me was for the OP, just clarifying that it wasn't a typo, it was just for your platform. – Jeff Storey May 12 '11 at 20:52
1
java -cp <complete path for your supporting jar>;<your jar which you want to run>

(for safety put both jars in the same folder)

Bhushan
  • 18,329
  • 31
  • 104
  • 137