1. 程式人生 > >jar命令

jar命令

point vid and run classes 選項 方式 tin over

jar 是java的一個壓縮文件名 (java archive),但是格式仍是zip的,所以你可以用winzip等支持zip格式的軟件打開,如果你是想自己生成一個jar文件,你可以用 jar工具來做。

官方文檔: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application‘s entry point. You provide this information with the Main-Class

header in the manifest, which has the general form:

Main-Class: classname

The value classname is the name of the class that is your application‘s entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

After you have set the Main-Class

header in the manifest, you then run the JAR file using the following form of the java command:

java -jar JAR-name

The main method of the class specified in the Main-Class header is executed.

An Example

We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.

We first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: MyPackage.MyClass

When you run the JAR file with the following command, the main method of MyClass executes:

java -jar MyJar.jar

Setting an Entry Point with the JAR Tool

The ‘e‘ flag (for ‘entrypoint‘) creates or overrides the manifest‘s Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

java -jar app.jar

If the entrypoint class name is in a package it may use a ‘.‘ (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class

----------------------------------------------------------------------------------------------

在命令行打 jar
即可看到它的幫助:

用法:jar {ctxu}[vfm0M] [jar-文件] [manifest-文件] [-C 目錄] 文件名 ...
選項:
-c 創建新的歸檔
-t 列出歸檔內容的列表
-x 展開歸檔中的命名的(或所有的〕文件
-u 更新已存在的歸檔
-v 生成詳細輸出到標準輸出上
-f 指定歸檔文件名
-m 包含來自指定的清單(manifest〕文件的清單(manifest〕信息
-0 只存儲方式;未用ZIP壓縮格式
-M 不產生所有項的清單(manifest〕文件
-i 為指定的jar文件產生索引信息
-C 改變到指定的目錄,並且包含下列文件:
如果一個文件名是一個目錄,它將被遞歸處理。
清單(manifest〕文件名和歸檔文件名都需要被指定,按‘m‘ 和 ‘f‘標誌指定的相同順序。

示例1:將兩個class文件歸檔到一個名為 ‘classes.jar‘ 的歸檔文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:用一個存在的清單(manifest〕文件 ‘mymanifest‘ 將 foo/ 目錄下的所有
文件歸檔到一個名為 ‘classes.jar‘ 的歸檔文件中:
jar cvfm classes.jar mymanifest -C foo/ .

jar命令