JAR 类路径和外部 jar
我实际上
在使用 eclipse 时遇到了 2 个问题 ->导出项目为我的简单桌面 (GUI) 程序生成 jar 文件
它生成一个 jar 文件和一个 ant 脚本。
第一个问题: 双击时生成的 jar 工作正常。
当我使用生成的ant脚本生成jar时
就我自己来说,这是行不通的。 像这样的目标可能会出现什么问题(假设满足所有依赖项)
<target name="create_run_jar">
<jar destfile="G:/dev/myproj/myproj.jar">
<manifest>
<attribute name="Main-Class" value="view.myproj"/>
<attribute name="Class-Path" value=". myproj_lib/grouplayout.jar"/>
</manifest>
<fileset dir="G:/dev/myproj/bin"/>
</jar>
<delete dir="G:/dev/myproj/myproj_lib"/>
<mkdir dir="G:/dev/myproj/myproj_lib"/>
<copy file="G:/dev/.metadata/.plugins/org.dyno.visual.swing/layoutext/grouplayout.jar" todir="G:/dev/myproj/myproj"/>
</target>
//nevemind
//第二个问题:
//当我双击自动生成的 jar 文件时,程序启动并正常工作。
//当我从命令行执行 java myjar 时,我得到主类未找到异常..
//很奇怪吧?
I actualy have 2 problems
I use eclipse -> export project to generate a jar file for my simple desktop (GUI) program
It generates a jar file and an ant script.
first problem:
the generated jar works fine when double-clicked.
When I use the generated ant script to generate the jar
by myself, it doesn't work.
What can be wrong with a target like this (assuming that all dependencies are met)
<target name="create_run_jar">
<jar destfile="G:/dev/myproj/myproj.jar">
<manifest>
<attribute name="Main-Class" value="view.myproj"/>
<attribute name="Class-Path" value=". myproj_lib/grouplayout.jar"/>
</manifest>
<fileset dir="G:/dev/myproj/bin"/>
</jar>
<delete dir="G:/dev/myproj/myproj_lib"/>
<mkdir dir="G:/dev/myproj/myproj_lib"/>
<copy file="G:/dev/.metadata/.plugins/org.dyno.visual.swing/layoutext/grouplayout.jar" todir="G:/dev/myproj/myproj"/>
</target>
//nevemind
//Second problem:
//when I double click on the auto-generated jar file the program launches and works fine.
//when I do java myjar from the command-line I get main class not found exception..
//weird huh?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您将两种方式生成的JAR文件,使用
jar
命令将它们展开到临时目录中,然后使用递归方式使用diff
进行比较。然而,我怀疑@Pace已经指出了这个问题;即您在 Class-Path 清单条目中使用相对路径,这可能会导致问题。
I suggest that you take the JAR files generated the two ways, use the
jar
command to expand them into temporary directories, and then usediff
in recursive mode to compare them.However, I suspect that @Pace has put his finger on the problem; i.e. that you are using relative paths in the
Class-Path
manifest entry and this is liable to cause problems.java -jar
是执行 jar 的正确方法。java -jar <jar name>
is the proper way to execute a jar.ant 目标正在创建带有类路径属性的清单。如果您查看这些路径,您会发现它们是相对于当前目录的。当您从命令行执行 java -jar 时,您是否位于...
...目录中?
The ant target is creating a manifest with a classpath attribute. If you look at those paths you'll notice that they are relative to the current directory. When you execute java -jar from the command line are you in the...
...directory?