如何创建一个JAR文件

来源:互联网 发布:淘宝产品十年事 pdf 编辑:程序博客网 时间:2024/05/17 20:22


http://www.javacoffeebreak.com/faq/faq0028.html

问题

我如何创建一个JAR文件?

回答

Java存档(JAR)文件,允许开发者很多类打包成一个单一的文件.JAR文件,也可以使用压缩,所以这可以使applet和应用程序较小。

创建JAR文件是很容易的。简单的进入.class文件所在目录: 

jar -cvf myfile.jar *.class

如果您的应用程序或applet使用包,那么你需要做的事情有点不同。假设你的类在包mycode.games.CoolGame -您想改变上面的目录myCode的,键入以下内容:

jar -cvf myfile.jar .\mycode\games\CoolGame\*.class

现在,如果你有一个现有的JAR文件,并希望将其解压,你会键入以下

jar -xf myfile.jar


http://www.ntu.edu.sg/home/ehchua/programming/java/J9d_Jar.html

Jarring-Up Files in Packages

Java classes are often placed in packages. To jar up all the classes of a package, you must provide the proper sub-directory structure (as depicted by the package name). Recall that package name with '.' are mapped to sub-directory, e.g., the class file com.test.MyClass is stored as com\test\MyClass.class.

For example, the following command jar-up all the classes in mypackage and the image sub-directory. Take note that the jar command should be issued from the project root directory, i.e., the base directory of the packages.

> jar cvf hello.jar mypackage\*.class imagesadded manifestadding: mypackage/HelloJarInPackage$1.class(in = 1016) (out= 536)(deflated 47%)adding: mypackage/HelloJarInPackage$2.class(in = 440) (out= 303)(deflated 31%)adding: mypackage/HelloJarInPackage.class(in = 1931) (out= 1034)(deflated 46%)adding: images/(in = 0) (out= 0)(stored 0%)adding: images/high.png(in = 978) (out= 983)(deflated 0%)adding: images/muted.png(in = 839) (out= 844)(deflated 0%)

Creating JAR file using jar tool with package is clumpy! I shall describe how to create a JAR file via an IDE (such as Eclipse/NetBeans) in the following sections.

You can use the jar tool to inspect and manipulate JAR file. However, it is much easier to use a graphical ZIP program (such as WinZIP or WinRAR) to inspect and manipulate JAR file. Try openning a JAR file using WinZIP or WinRAR or any ZIP tool.