如何获得Java程序的可执行文件?
我正在尝试将用 Java 6 编写的程序导出到 JAR 文件。
我的项目包含一个来自 Internet 的 Java 库和一些 Java 源文件。 当我创建 JAR 文件时,应该默认设置类路径,并且最终用户应该能够直接从命令提示符或其他源运行项目。
我的目标是将所有内容导出到 JAR 文件(如果可能的话)。 此外,程序输出应该在命令提示符处给出。
我怎样才能以这种方式导出我的程序?
Possible Duplicate:
How can I convert my java program to an .exe file ?
I'm trying to export a program written in Java 6 to a JAR file.
My project contains one Java library from the Internet and some Java source files. When I create the JAR file, the classpath should be set by default and the end-user should be able to run the project directly from either the command prompt or some other source.
My goal is to export everything to a JAR file, if that's possible. Also, the program output should be given at the command prompt.
How can I export my program in this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要为此编写一个
META-INF/MANIFEST.MF
文件; 该文件应该位于您正在分发的 jar 文件中(即人们与java -jar
一起使用的文件)。 这是我最近编写的一个“项目”的示例:需要关心的主要字段是 Main-Class(它指定要查找
main
方法的类),以及Class-Path
(指定您正在使用的外部 jar 文件)。You need to write a
META-INF/MANIFEST.MF
file for this; the file should sit within the jar file you're distributing (i.e., the one that people use withjava -jar
). Here's an example for a recent "project" I wrote:The main fields to care about are
Main-Class
(which specifies which class to look for yourmain
method), andClass-Path
(which specifies the external jar files you're using).您可以使用 Jsmooth http://jsmooth.sourceforge.net/ 捆绑您的 java 代码以生成可执行文件。
You can use Jsmooth http://jsmooth.sourceforge.net/ to bundle your java code to make an executable.
我对两种不同的方法有很好的经验:
1)使用 fatjar 插件和 eclipse 来创建一个包含您需要嵌入的所有 jar 的单个 jar 文件(使用自定义类加载器)。 这将创建一个可单击的 jar 文件(如果它是 GUI 程序)。 如果您需要此 Jar 的控制台输出,请按照 Bhushan 的描述用 jsmooth 包装它。
2) 使用 Eclipse 3.5 Milestone 6 中的“Export -> Runnable Jar”和“将依赖库复制到子文件夹”选项来创建一个普通的可运行 jar,其中包含引用依赖 jar 文件(放入子文件夹中)的正确清单)。 如果您想要一个不使用任何技巧的解决方案,这非常有效,但编写脚本有点困难。
I have had good experiences with two different approaches:
1) Use the fatjar plugin with eclipse to create a single jar file with all the jars you need embedded (uses a custom classloader). This creates a clickable jar file (if it is a GUI program). If you need console output for this Jar wrap it with jsmooth as described by Bhushan.
2) Use the "Export -> Runnable Jar" in Eclipse 3.5 Milestone 6 with the "copy dependent libraries to a subfolder" option to create an ordinary runnable jar with a proper manifest referencing the dependent jar files (which are put in a subfolder). This works very well if you want a solution not using any tricks, but it is a bit hard to script.