In CMD, we can use the JDK's command: jar. (You have to first of all configure correctly the CMD environment variables)
1. To pack two .class files into an archive named testPackage.jar
jar cvf testPackage.jar classFile1.class classFile2.class
2. To pack two .class files into an archive named testPackage2.jar with the usage of a existant manifest file "manifestForTest.MF"
jar cvfm testPackage2.jar manifestForTest.MF classFile1.class classFile2.class
3. To pack all the .class files in a folder specified into an archive named testPackage3.jar with the usage of a existant manifest file "manifestForTest.MF"
jar cvfm testPackage3.jar manifestForTest.MF -C folderPath
示例:
1. 清单文件manifest,对格式要求很高,注意每个冒号后面要有一个空格,文件最后要有一个空行。
2. 对于引用到外部jar包的工程,需要在manifeste中定义好Class-Path属性。
In Eclipse, you can use the related GUI guide for creating .jar file. But I encountered a problem : the eclipse GUI guide can not see the images files in the same level folder as the class files. (The images can thus be easily accessed by this.getClass().getResource()
in the java codes.) So in that case we have no choice but to use the commands.
For executing .jar files, you have only to use the command: java -jar jarFile ...
If you want to see the informations output by System.out.println(), you can type: java -jar jarFile > log.txt
You can also use a .bat file to process, in the .bat file: java -jar jarFile > log.txt
总之,你首先要思考的就是:Jar是在别人机器也可以运行的,所以就知道需要打包除标准JRE中自带的API(更广泛的说是在JDK中的src.zip)之外所有资源。