Possible Duplicate:
How can I convert my java program to an .exe file ?可能重复:如何将我的java程序转换为.exe文件?
I'm trying to export a program written in Java 6 to a JAR file.
我正在尝试将用Java 6编写的程序导出到JAR文件。
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.
我的项目包含一个来自Internet的Java库和一些Java源文件。当我创建JAR文件时,默认情况下应该设置类路径,最终用户应该能够直接从命令提示符或其他来源运行项目。
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.
我的目标是将所有内容导出到JAR文件,如果可能的话。此外,程序输出应在命令提示符下给出。
How can I export my program in this way?
如何以这种方式导出我的程序?
3 个解决方案
#1
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 with java -jar
). Here's an example for a recent "project" I wrote:
您需要为此编写META-INF / MANIFEST.MF文件;该文件应位于您正在分发的jar文件中(即人们使用java -jar的文件)。这是我写的最近“项目”的一个例子:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 11.3-b02 (Sun Microsystems Inc.)
Main-Class: com.hedgee.simonsays.Main
Class-Path: lib/libthrift.jar
The main fields to care about are Main-Class
(which specifies which class to look for your main
method), and Class-Path
(which specifies the external jar files you're using).
要关注的主要字段是Main-Class(指定要查找主方法的类)和Class-Path(指定您正在使用的外部jar文件)。
#2
You can use Jsmooth http://jsmooth.sourceforge.net/ to bundle your java code to make an executable.
您可以使用Jsmooth http://jsmooth.sourceforge.net/捆绑您的Java代码以生成可执行文件。
#3
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.
1)使用带有eclipse的fatjar插件创建一个包含所有嵌入的jar文件的jar文件(使用自定义类加载器)。这将创建一个可点击的jar文件(如果它是一个GUI程序)。如果您需要这个Jar的控制台输出,请使用如Bhushan所描述的jsmooth。
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.
2)使用Eclipse 3.5 Milestone 6中的“Export - > Runnable Jar”和“将依赖库复制到子文件夹”选项创建一个普通的可运行jar,其中包含引用相关jar文件的正确清单(放在子文件夹中) 。如果你想要一个不使用任何技巧的解决方案,这非常有效,但是脚本有点难。
#1
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 with java -jar
). Here's an example for a recent "project" I wrote:
您需要为此编写META-INF / MANIFEST.MF文件;该文件应位于您正在分发的jar文件中(即人们使用java -jar的文件)。这是我写的最近“项目”的一个例子:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 11.3-b02 (Sun Microsystems Inc.)
Main-Class: com.hedgee.simonsays.Main
Class-Path: lib/libthrift.jar
The main fields to care about are Main-Class
(which specifies which class to look for your main
method), and Class-Path
(which specifies the external jar files you're using).
要关注的主要字段是Main-Class(指定要查找主方法的类)和Class-Path(指定您正在使用的外部jar文件)。
#2
You can use Jsmooth http://jsmooth.sourceforge.net/ to bundle your java code to make an executable.
您可以使用Jsmooth http://jsmooth.sourceforge.net/捆绑您的Java代码以生成可执行文件。
#3
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.
1)使用带有eclipse的fatjar插件创建一个包含所有嵌入的jar文件的jar文件(使用自定义类加载器)。这将创建一个可点击的jar文件(如果它是一个GUI程序)。如果您需要这个Jar的控制台输出,请使用如Bhushan所描述的jsmooth。
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.
2)使用Eclipse 3.5 Milestone 6中的“Export - > Runnable Jar”和“将依赖库复制到子文件夹”选项创建一个普通的可运行jar,其中包含引用相关jar文件的正确清单(放在子文件夹中) 。如果你想要一个不使用任何技巧的解决方案,这非常有效,但是脚本有点难。