How can include multiple jars in the classpath using ant?

James picture James · Dec 22, 2009 · Viewed 22.4k times · Source

I have a bunch of .java files in a "src" folder that depend on three jars in a "lib" folder. I have the following build.xml file:

<?xml version="1.0"?>
<project name="MyProj" basedir=".">
 <property name="src"   value="src"/>
 <property name="build" value="build"/>
 <property name="lib"   value="lib"/>


 <path id="master-classpath">
   <fileset dir="${lib}">
     <include name="activemq-all-5.1-SNAPSHOT.jar"/>
     <include name="geronimo-jms_1.1_spec-1.1.1.jar"/>
     <include name="activemq-core-5.3.0.jar"/>
   </fileset>
 </path>

 <javac destdir="${build}">
   <src path="${src}"/>
   <classpath refid="master-classpath"/>
 </javac>

</project>

This compiles fine, but when I try and run I get

"java.lang.NoClassDefFoundError: javax/jms/Destination"

This program runs and compiles fine when I include the jars in the buildpath using Eclipse, though.

EDIT: So I copied the jars into the folder that has the compiled classes. The class with the main method is NDriver.class. When I try:

java -classpath ./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar NDriver

This gives:

Exception in thread "main" java.lang.NoClassDefFoundError: NDriver

I'd appreciate any help.

Answer

Pascal Thivent picture Pascal Thivent · Dec 23, 2009

You need to put the jars used at compile time on the classpath when running the application. Sadly, you didn't provide any detail on how you are actually running it so its hard to provide more guidance.

UPDATE: The directory containing the compiled classes needs to be added to the classpath too. If you launch java from the directory containing the compiled classes, then you can use . to designate the current directory. Add it to the classpath as shown below to tell java to look for classes there too (I've added . right after activemq-all-5.1-SNAPSHOT.jar):

java -classpath ./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar:. NDriver