命令行方法:
1、 创建Manifest.txt文件,内容:
Main-Class: com.mkyong.awt.AwtExample
2、打包所有的class,包括Manifest.txt文件:
$ jar -cvfm run.jar Manifest.txt com/
3、run.jar即可在windows下双击鼠标执行(前提是要安装jre)。在linux用命令行执行:
$ java -jar run.jar
IDE方法:
用eclipse
1、右键点击工程,选择 export... 菜单:
2、选择 java 下面的 Runnable JAR file:
3、在launch configuration里选择包含main函数的类
-
Extract required libraries into JAR - Extracts the actual
.class
files from the libraries your app uses and puts those.class
files inside the runnable JAR. So, the runnable JAR will not only contain the.class
files of your application, but also the.class
files of all the libraries your application uses. -
Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible.
-
Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the
.class
files of your application.
-
一般情况下,选择2.
4、用launch4j可以给程序定制图标等东西。
打包好的jar文件,可以用 jar tf run.jar 查看其中的内容