蚂蚁+类路径问题

发布于 2024-09-13 17:22:37 字数 2284 浏览 6 评论 0原文

我已经编写了一个 ANT 脚本,最后正在构建 jar,

这里是 jar 部分的构建

<jar jarfile="${destination}/@{name}.jar">
     <fileset dir="${output}">
         <include name="abc/xyz/@{name}/**"/>
     </fileset>
     <zipfileset dir="lib" prefix="lib/"/>
     <manifest>
          <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
          <attribute name="Class-Path" value=".:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar"/>
     </manifest>
</jar>

现在,当我尝试执行它执行的包时,但每当发送数据时,我都会收到错误。

java.lang.NoClassDefFoundError: javax/persistence/NoResultException

但是持久性 jar 存在于类路径中,我已经尝试了类路径的所有组合,但徒劳。

但在另一个系统中,我尝试不创建像这样的罐子,

set classpath=.;lib/activation.jar;lib/antlr-2.7.6.jar;lib/asm-attrs.jar;lib/asm.jar;lib/cglib-2.1.3.jar;lib/commons-collections-2.1.1.jar;lib/commons-logging-1.1.jar;lib/dom4j-1.6.1.jar;lib/ehcache-1.2.3.jar;lib/ejb3-persistence.jar;lib/hibernate-annotations.jar;lib/hibernate-commons-annotations.jar;lib/hibernate-entitymanager.jar;lib/hibernate-tools.jar;lib/hibernate3.jar;lib/javassist.jar;lib/jdbc2_0-stdext.jar;lib/jta.jar;lib/mysql-connector-java-5.1.5-bin.jar
java com.abc.xyz.HelloWorld

效果很好。

在 Mac 中,当我这样尝试时:

java -cp .:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar com.abc.xyz.HelloWorld

它也工作正常:(,但是在我创建 jar 的那一刻它就停止了。

我该如何解决这个问题?

I have written an ANT script and finally am building the jar

here is the building of jar section

<jar jarfile="${destination}/@{name}.jar">
     <fileset dir="${output}">
         <include name="abc/xyz/@{name}/**"/>
     </fileset>
     <zipfileset dir="lib" prefix="lib/"/>
     <manifest>
          <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
          <attribute name="Class-Path" value=".:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar"/>
     </manifest>
</jar>

Now when I try to execute the package it executes, but whenever data is sent I get an error.

java.lang.NoClassDefFoundError: javax/persistence/NoResultException

But the persistence jar is there in the class-path, I've tried all the combinations for the class-path, but in vain.

But in another system I tried without create a jar like

set classpath=.;lib/activation.jar;lib/antlr-2.7.6.jar;lib/asm-attrs.jar;lib/asm.jar;lib/cglib-2.1.3.jar;lib/commons-collections-2.1.1.jar;lib/commons-logging-1.1.jar;lib/dom4j-1.6.1.jar;lib/ehcache-1.2.3.jar;lib/ejb3-persistence.jar;lib/hibernate-annotations.jar;lib/hibernate-commons-annotations.jar;lib/hibernate-entitymanager.jar;lib/hibernate-tools.jar;lib/hibernate3.jar;lib/javassist.jar;lib/jdbc2_0-stdext.jar;lib/jta.jar;lib/mysql-connector-java-5.1.5-bin.jar
java com.abc.xyz.HelloWorld

This works fine.

In Mac when I try like this:

java -cp .:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar com.abc.xyz.HelloWorld

Also it works fine :(, but the minute I create the jar it stops.

How can I resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

决绝 2024-09-20 17:22:37

首先,“Main-Class”和“Class-Path”清单条目仅用于可执行的 jar。换句话说,当您按如下方式调用 java 时:

java -jar foo.jar

使用 -cp 选项调用 java 意味着您正在提供自己的类路径,并且请注意,您还必须在命令行上提供主类。

其次,您需要将“:”字符替换为空格:

<attribute name="Class-Path" value=". lib/activation.jar lib/antlr-2.7.6.jar lib/asm-attrs.jar lib/asm.jar lib/cglib-2.1.3.jar lib/commons-collections-2.1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-1.2.3.jar lib/ejb3-persistence.jar lib/hibernate-annotations.jar lib/hibernate-commons-annotations.jar lib/hibernate-entitymanager.jar lib/hibernate-tools.jar lib/hibernate3.jar lib/javassist.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/mysql-connector-java-5.1.5-bin.jar"/>

最后,我建议使用 ma​​nifestclasspath 任务来为您构建类路径字符串。它将正确解析您的 jar 与其运行时依赖项之间的任何相关链接。

<manifestclasspath property="mf.classpath" jarfile="${destination}/@{name}.jar">
    <classpath>
        <fileset dir="lib" includes="*.jar"/>
    <classpath>
</manifestclasspath>

<jar jarfile="${destination}/@{name}.jar">
    <fileset dir="${output}">
       <include name="abc/xyz/@{name}/**"/>
    </fileset>
    <zipfileset dir="lib" prefix="lib/"/>
    <manifest>
       <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
       <attribute name="Class-Path" value=". ${mf.classpath}"/>
    </manifest>
</jar>

最后一个观察...为什么要在 jar 中包含 lib 目录的内容? (jar 命令中的 zipfileset 标记?)

这似乎是不必要的,您所需要做的就是确保运行时依赖项存在于类路径清单条目中指定的 lib 目录中。

First of all the "Main-Class" and "Class-Path" manifiest entries is only used for executeable jars. In other words when you invoke java as follows:

java -jar foo.jar

Invoking java using the -cp option means you're supplying your own classpath and note that you also have to provide the main class on the command line as well.

Secondly you need to replace the ":" characters with spaces:

<attribute name="Class-Path" value=". lib/activation.jar lib/antlr-2.7.6.jar lib/asm-attrs.jar lib/asm.jar lib/cglib-2.1.3.jar lib/commons-collections-2.1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-1.2.3.jar lib/ejb3-persistence.jar lib/hibernate-annotations.jar lib/hibernate-commons-annotations.jar lib/hibernate-entitymanager.jar lib/hibernate-tools.jar lib/hibernate3.jar lib/javassist.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/mysql-connector-java-5.1.5-bin.jar"/>

Finally I'd recommend using the manifestclasspath task to build your classpath string for you. It will correcly resolve any relative links between your jar and it's run-time dependencies.

<manifestclasspath property="mf.classpath" jarfile="${destination}/@{name}.jar">
    <classpath>
        <fileset dir="lib" includes="*.jar"/>
    <classpath>
</manifestclasspath>

<jar jarfile="${destination}/@{name}.jar">
    <fileset dir="${output}">
       <include name="abc/xyz/@{name}/**"/>
    </fileset>
    <zipfileset dir="lib" prefix="lib/"/>
    <manifest>
       <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
       <attribute name="Class-Path" value=". ${mf.classpath}"/>
    </manifest>
</jar>

One final observation.... Why are you including the contents of the lib directory inside the jar? (The zipfileset tag in the jar command?)

This appears unnecessary, all you need to do is ensure that the run-time dependencies are present in a lib directory as specified in your Class-Path manifest entry.

雨巷深深 2024-09-20 17:22:37

它不起作用的原因是类路径中没有,解释如下:
Java-Jar-Ignores-Classpath-Workaround

The reason for it not works was there not in the class path, the explanation is given here:
Java-Jar-Ignores-Classpath-Workaround

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