如何使用 ant 在类路径中包含多个 jar?
我在“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>
这编译得很好,但是当我尝试运行时,我得到
“java.lang.NoClassDefFoundError: javax/jms/目标”
不过,当我使用 Eclipse 将 jar 包含在构建路径中时,该程序可以正常运行和编译。
编辑:所以我将 jar 复制到包含已编译类的文件夹中。具有 main 方法的类是 NDriver。当我尝试时:
java-类路径 ./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar NDriver
这给出:
线程“main”中出现异常 java.lang.NoClassDefFoundError: NDriver
我将不胜感激任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当运行应用程序时,您需要将编译时使用的 jar 放在类路径中。遗憾的是,您没有提供有关实际运行方式的任何详细信息,因此很难提供更多指导。
更新: 包含已编译类的目录也需要添加到类路径中。如果从包含已编译类的目录启动
java
,则可以使用.
指定当前目录。将其添加到类路径中,如下所示,告诉java
也在那里查找类(我已在activemq-all-5.1-SNAPSHOT 之后添加了
.
。罐子):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 telljava
to look for classes there too (I've added.
right afteractivemq-all-5.1-SNAPSHOT.jar
):一种方式(与您的变量略有不同)
当然,这里我假设您正在创建一个 jar 并运行它(包括那里的类路径)。另一种选择是使用
run
目标,该目标使用
标记并显式使用其中的类路径。One way (slightly different variables than yours)
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 是否包含在类路径中? Eclipse 会自动添加这些,但是您需要在从命令行运行程序时指定它们。
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.
根据我的经验,Eclipse 通常会在类路径中包含类和 jar,而无需显式使用类路径声明。事实上,有时从 Eclipse 的构建中删除类非常困难(必须删除或清理它们)。
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).