1 使用JAR命令
1)创建JAR文件
The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)
options and arguments:
c: indicates to create a JAR file.
f: indicates output to a file,如果是文件夹,则会将整个文件下的内容(包括子目录)打包,文件名中可使用通配符*
other Options and arguments
v: produces output and std output
0: indicates not to compress the jar file
M: indicates that the default manifest file should not be produced
m: using existing manifest file
jar cmf existing-manifest jar-file input-file(s)
-C: To change directories during execution of the command
2) To view the content of a JAR file jar tf jar-file
t: indicates to view the table of contents of jar file
3) To extract the contents of a JAR file jar xf jar-file
x: indicates to extract jar file or specific files
4) To extract specific files from a JAR file jar xf jar-file archived-file(s)
5) To run an application packaged as a JAR file(requires Main-Class manifest header)
java -jar app.jar
关于MANIFEST.MF文件
创建JAR文件的时候,将会自动生成一个默认的manifest文件,一个jar包中只能有一个manifest文件,并只能在META-INF/MANIFEST.MF路径中
默认的manifest文件包括以下的内用
Manifest-Version: 1.0 //版本号
Created-By: 1.4.2_07 (Sun Microsystems Inc.)
一些特殊用途的MANIFEST头
JAR应用程序:
Main-Class: 定义jar中应用程序的入口class, 用java -jar app.jar时将会直接调用该class中的public static void main(String[] args)方法,如下:
Main-Class: com.cmm.test.ServerTest
使用第三方jar文件,如下
Class-Path: servlet.jar infobus.jar ./lib/beans.jar 运行该jar包时,将会搜索该jar文件相同目录下的servlet.jar, infobus.jar以及子目录lib下的beans.jar文件。
版本信息 如下格式
Name: java/util/
Specification-Title: "Java Utility Classes"
Specification-Version: "1.2"
Specification-Vendor: "Sun Microsystems, Inc.".
Implementation-Title: "java.util"
Implementation-Version: "build57"
Implementation-Vendor: "Sun Microsystems, Inc."
一个包含入口class和第三方jar package的MANIFEST.MF文件如下
Manifest-Version: 1.0
Created-By: 1.4.2_07 (Sun Microsystems Inc.)
Main-Class: com.cmm.test.ServerTest
Class-Path: ./log4j_128.jar ./lib/log4j_128.jar