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

点点圈 提交于 2019-12-01 10:11:50

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

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

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.

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