How to enable multiple jars in classpath using ant?

I have a bunch of .java files in the "src" folder, which depends on the three jars in the "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 to run, I get

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

This program starts and compiles fine when I include banks in the build path using Eclipse.

EDIT: So I copied the banks to a folder in which there are 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

.

+3
4

, , . , - , , .

: , , . java , , . . , , java ( . 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
+3

( , )

<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>

, , ( ). run, <java> .

+2

? Eclipse , .

+1
source

In my experience, it seems that Eclipse will often include classes and jars in the classpath without explicitly using the classpath declaration. Indeed, it is sometimes difficult to remove classes from the Eclipse assembly (they need to be removed or cleared).

0
source

Source: https://habr.com/ru/post/1726392/


All Articles