1

I have two jar files, A.jar and B.jar. A contains my main function and it relies on classes defined in B.

I put A and B in the same folder, which is in the Windows PATH, and try and run my main in A...

java -jar A.jar -dosomething

This gives me a java.lang.NoClassDefFound error on a method of B that I call in the code underneath -dosomething. Interestingly it has found A.jar and called the main method, but it can't find B.jar, even though it is in the same folder as A.jar.

So I read around (again) about CLASSPATHs and the JAVA_HOME variable and I made sure that my JAVA_HOME is pointing to the right place. This document describes how I should be able to put my extension jar file(s), in my case B, into the /lib/ext folder, so I tred that, but I get the same java.lang.NoClassDefFound error.

I am using Java jdk1.5.0_10, so for good measure I also placed B.jar in the jre/lib/ext folder underneath there.

So now I have it in three places, the local folder on the PATH, JAVA_HOME/lib/ext and JAVA_HOME/jre/lib/ext and I still get the same error.

Should using lib/ext work? Where on earth have I got to put my B.jar file for this to work properly?

Simon
  • 78,655
  • 25
  • 88
  • 118

5 Answers5

3

JAVA_HOME is not a environment variable used by Java. It's used by other tools such as Ant, but not by Java.

The PATH environment variable is not used by Java either. It's used to find executables in windows, but Java doesn't use it to find jars.

Placing two jars in the same directory doesn't make them automatically in the same classpath. If A.jar is an executable jar (as it seems to be), and it depends on an external B.jar, then its Class-Path entry of its manifest must reference B.jar. Read http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thanks, how about putting them in the lib/ext? I don't think the jar manifest can be the only way – Simon Jan 07 '12 at 21:10
  • See more about JAVA_HOME here: http://stackoverflow.com/questions/2025290/what-is-java-home-how-does-jvm-will-find-the-javac-path-stored-in-java-home – Dan Hardiker Jan 07 '12 at 21:12
  • Wow same answers, but you were faster to write :P – Adel Boutros Jan 07 '12 at 21:13
  • I started writing mine 8 minutes ago, but you were faster :P – Adel Boutros Jan 07 '12 at 21:14
  • Putting application jar files in lib/ext is very bad practice. The JRE can be used by many other applications than yours, and they don't need to have your classes in their classpath. – JB Nizet Jan 07 '12 at 21:16
2

You need to add a manifest file to your A.jar file like this.

The contents of the manifest file should look like this, where the Main-Class points to your package name and the class name of your main class in A.

Save this in a file named something like Manifest.mf

Manifest-Version: 1.0
Main-Class: com.your.package.name.A
Class-Path: A.jar B.jar

You can manually add this file to your jar by changing your .jar file to a .zip then opening it and modifying the contents of the existing manifest file if there is one or copying the Manifest.mf file into your jar/zip file and changing it back to a .jar file.

Logan
  • 2,369
  • 19
  • 20
  • There should be a Meta-Inf folder inside of your jar, the Manifest.mf file should be in that folder of the jar. – Logan Jan 08 '12 at 00:53
0

To make executable-jar file you need to add a manifest file which contains the Main-class name and other dependent class paths.

Manifest.txt

Main-class: A
Class-Path: B.jar

Add this manifest file into A.jar

cmd:/folderLocation>jar cvfm A.jar manifest.txt A.class

place both jars in one folder and run A.jar using cmd java -jar A.jar

0

You need to have a Manifest file to be able to call classes from another jar. Check this link

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
0

I would consider using a build system like Ant or Maven, my personal preference is for Maven. Most have plugins to take care of the complex classpath / dependency issue - in Maven, that's the Shade plugin.

I would do Hello World with a Maven example. Most modern IDEs (IDEA, Eclipse, NetBeans) can generate projects from a pom.xml.

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Dan Hardiker
  • 3,013
  • 16
  • 19