views:

424

answers:

4

Hi, 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.

A: 

One way (slightly different variables than yours)

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<manifestclasspath property="manifest.classpath" jarfile="${jarfile}">
    <classpath refid="classpath"/>
</manifestclasspath>

<target name="jar" depends="compile" description="create the jar">
    <jar destfile="${jarfile}" basedir="${build.dir}">
        <manifest>
            <attribute name="Manifest-Version" value="${manifest-version}"/>
            <attribute name="Created-By" value="${ant.java.version}"/>
            <attribute name="Main-Class" value="${main-class}"/>
            <attribute name="Class-Path" value="${manifest.classpath}"/>
        </manifest>
    </jar>
</target>

Of course here I'm assuming that you are creating a jar and running it (including the classpath there). Another option would be to have a run target which use the <java> tag and explicitly use the classpath there.

Davide
+3  A: 

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
Pascal Thivent
If you're using Java 6 and if the jars are all in the same directory you could use a wildcard too. Something like: java -cp ./*:. NDriver
Jeremy Raymond
A: 

From my experience it seems Eclipse will often include classes and jars in the classpath without explicitly using the classpath declaration. Indeed it can sometimes be quite hard to remove classes from Eclipse's build (they have to be deleted or clean'ed).

peter.murray.rust
+1  A: 

Are the library jars included in the classpath when you run the program? Eclipse automatically add these, but you need to specifying them when you run the program from the command line.

akr