Java 如何使用 ant 在类路径中包含多个 jars?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1949738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can include multiple jars in the classpath using ant?
提问by James
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:
我在“src”文件夹中有一堆 .java 文件,它们依赖于“lib”文件夹中的三个 jar。我有以下 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 在构建路径中包含 jars 时,该程序运行并编译得很好。
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:
编辑:所以我将 jars 复制到包含已编译类的文件夹中。带有 main 方法的类是 NDriver.class。当我尝试:
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.
我很感激任何帮助。
采纳答案by Pascal Thivent
You need to put the jars used at compile time on the classpath when runningthe application. Sadly, you didn't provide any detail on how you are actually running it so its hard to provide more guidance.
运行应用程序时,您需要将编译时使用的 jar 放在类路径上。遗憾的是,您没有提供有关实际运行方式的任何详细信息,因此很难提供更多指导。
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
从包含已编译类的目录启动,则可以使用.
来指定当前目录。将它添加到类路径如下所示告诉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
回答by Davide
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.
当然,这里我假设您正在创建一个 jar 并运行它(包括那里的类路径)。另一种选择是拥有一个run
使用<java>
标记并在那里显式使用类路径的目标。
回答by peter.murray.rust
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).
根据我的经验,Eclipse 似乎经常在类路径中包含类和 jar,而不显式使用类路径声明。事实上,有时很难从 Eclipse 的构建中删除类(它们必须被删除或清理)。
回答by Andreas Kraft
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.
运行程序时,类路径中是否包含库 jars?Eclipse 会自动添加这些,但您需要在从命令行运行程序时指定它们。