How can I get an executable file of a Java program? [duplicate]

和自甴很熟 提交于 2019-12-01 09:03:15

问题


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?


回答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:

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).




回答2:


You can use Jsmooth http://jsmooth.sourceforge.net/ to bundle your java code to make an executable.




回答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.

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.



来源:https://stackoverflow.com/questions/743029/how-can-i-get-an-executable-file-of-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!