简体   繁体   English

如何在类路径中使用ant包含多个jar?

[英]How can include multiple jars in the classpath using ant?

I have a bunch of .java files in a "src" folder that depend on three jars in a "lib" folder. 我在“src”文件夹中有一堆.java文件,这些文件依赖于“lib”文件夹中的三个jar。 I have the following build.xml file: 我有以下build.xml文件:

<?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" “java.lang.NoClassDefFoundError:javax / jms / Destination”

This program runs and compiles fine when I include the jars in the buildpath using Eclipse, though. 但是,当我使用Eclipse在jar路径中包含jar时,这个程序运行并编译得很好。

EDIT: So I copied the jars into the folder that has the compiled classes. 编辑:所以我将jar复制到具有编译类的文件夹中。 The class with the main method is NDriver.class. 主方法的类是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 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 线程“main”中的异常java.lang.NoClassDefFoundError:NDriver

I'd appreciate any help. 我很感激任何帮助。

You need to put the jars used at compile time on the classpath when running the application. 运行应用程序时,需要将编译时使用的jar放在类路径 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 . 如果从包含已编译类的目录启动java ,则可以使用. 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也在那里查找类(我已经添加.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

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). 当然,我假设您正在创建一个jar并运行它(包括那里的类路径)。 Another option would be to have a run target which use the <java> tag and explicitly use the classpath there. 另一种选择是使用一个run目标,该目标使用<java>标记并在那里显式使用类路径。

Are the library jars included in the classpath when you run the program? 运行程序时,库jar是否包含在类路径中? Eclipse automatically add these, but you need to specifying them when you run the program from the command line. Eclipse会自动添加这些,但您需要在从命令行运行程序时指定它们。

From my experience it seems Eclipse will often include classes and jars in the classpath without explicitly using the classpath declaration. 根据我的经验,似乎Eclipse通常会在类路径中包含类和jar,而不显式使用类路径声明。 Indeed it can sometimes be quite hard to remove classes from Eclipse's build (they have to be deleted or clean'ed). 实际上,有时很难从Eclipse的构建中删除类(它们必须被删除或清除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM