jar 文件无效或损坏
我尝试从 java 项目创建一个 jar 文件,该项目使用一些外部 jar。 我创建了一个 lib 文件夹并将我需要的所有 jar 放在那里。
我通过将 lib 文件夹中的所有 jar 添加到构建路径来在 eclipse 中运行该项目,并且工作正常。
当我尝试使用 build.xml 中的 ant 创建 jar 时,看起来没问题,没有显示错误。
当我尝试运行 jar 时,收到消息“无效或损坏的 jarfile”。
在 build.xml 中: 我定义了用于编译的路径:
<path id="project.classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
这是编译的目标:
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}">
<classpath refid="project.classpath"/>
</javac>
</target>
这是制作 jar 文件的目标:
<target name="dist" depends="compile" description="generate the distribution" >
<mkdir dir="${dist}"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="${main}" />
<attribute name="Class-Path:" value="lib/**/*.*"/>
</manifest>
<fileset dir="${src}" includes="images/**/*.*" />
</jar>
<echo file="${dist}/start.bat" message="java -jar MyProject-${DSTAMP}.jar" />
</target>
你能告诉我我做错了什么吗?
I tried to create a jar file from a java project, which uses some external jars. I created a lib folder and put all the jars I need there.
I run the project in eclipse by adding all the jars in the lib folder to the Build Path and it works ok.
When I try to create the jar with ant from build.xml, it seems ok, no error is shown.
When I try to run the jar, I get the message "Invalid or corupt jarfile".
In build.xml:
I define the path to use for compiling:
<path id="project.classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
This is the target for the compilation:
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}">
<classpath refid="project.classpath"/>
</javac>
</target>
And this is the target for making the jar file:
<target name="dist" depends="compile" description="generate the distribution" >
<mkdir dir="${dist}"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="${main}" />
<attribute name="Class-Path:" value="lib/**/*.*"/>
</manifest>
<fileset dir="${src}" includes="images/**/*.*" />
</jar>
<echo file="${dist}/start.bat" message="java -jar MyProject-${DSTAMP}.jar" />
</target>
Can you please tell me what have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先删除
Class-Path:
之后的冒号以匹配然后我建议阅读
如何从 Ant 创建 MANIFEST.MF 类路径 或更好地使用 清单类路径
First remove the colon after
Class-Path:
to matchThen I suggest reading
HOWTO Create MANIFEST.MF Classpath From Ant or better using Manifestclasspath
我认为您的 Class-Path 属性不应在
build.xml
中指定尾随冒号。尝试
从命令行使用,看看是否可以扩展您的 jar 文件,以及它是否包含您期望的内容(上面将简单地转储目录,但这是一个有用的检查)
编辑:更改为反映下面的反馈
I don't think your Class-Path attribute should have a trailing colon specified in your
build.xml
.Try using
from the command line, and see if that can expand your jar file, and whether it contains what you expect (the above will simply dump the table of contents, but is a useful check)
EDIT: Changed to reflect the feedback below