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

发布于 2024-08-16 00:59:00 字数 1236 浏览 5 评论 0原文

我在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方圜几里 2024-08-23 00:59:00

运行应用程序时,您需要将编译时使用的 jar 放在类路径中。遗憾的是,您没有提供有关实际运行方式的任何详细信息,因此很难提供更多指导。

更新: 包含已编译类的目录也需要添加到类路径中。如果从包含已编译类的目录启动 java,则可以使用 . 指定当前目录。将其添加到类路径中,如下所示,告诉 java 也在那里查找类(我已在 activemq-all-5.1-SNAPSHOT 之后添加了 .。罐子):

java -classpath ./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar:. NDriver

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
一城柳絮吹成雪 2024-08-23 00:59:00

一种方式(与您的变量略有不同)

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

当然,这里我假设您正在创建一个 jar 并运行它(包括那里的类路径)。另一种选择是使用 run 目标,该目标使用 标记并显式使用其中的类路径。

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.

迟到的我 2024-08-23 00:59:00

运行程序时,库 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.

风启觞 2024-08-23 00:59:00

根据我的经验,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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文