jar命令打jar包


目錄:

1. 准備工作

2. jar命令打jar包

  1) jar命令用法

  2)第一種方式 jar -cvf ./class2json.jar ./com  (這種方法不行)

  3)第二種方式  jar cvfm jar包名稱 主類清單文件 要打包的class文件

  4) 用壓縮軟件進行壓縮

3)以及4)方法生成的jar包,需要和class2json3_lib放在同一文件下才能運行

 

1. 准備工作:

為了省事,這里以eclipse中項目Class2Json為例,該項目結構如下:

bin:是根據執行.java生成的.class文件,bin下的目錄與src目錄一樣,只不過是.class文件
lib:外部依賴包
src:我們自己寫的.java文件

 

接下來:

1. 我們新建一個Class2Json_jar的文件夾
2. 把bin下的文件復制到Class2Json_jar
3. 並把lib整個復制過去,把lib改名為class2json3_lib
4. 創建META-INF文件夾,並在該文件夾下創建MANIFEST.MF文件

在MANIFEST.MF文件中添加如下內容:(書寫注意規范,中間都要用一個空格隔開,即使下一行,開頭也要一個空格)

(第一個是版本號; 第二個是額外的jar包,第三個是啟動的主類)

Manifest-Version: 1.0
Class-Path: . class2json3_lib/commons-beanutils-1.8.0.jar class2json3_
 lib/commons-collections-3.2.1.jar class2json3_lib/commons-lang-2.4.ja
 r class2json3_lib/commons-logging-1.1.3.jar class2json3_lib/ezmorph-1
 .0.6.jar class2json3_lib/json-lib-2.3-jdk15.jar
Main-Class: com.zc.class2json.trans.test.TestDataClass2Json

 我的Class2Json_jar文件夾包含如下內容:

 

2. jar命令打jar包

1) jar命令用法

zhangchao@zc:~/zc/jar-package-example/Class2Json_jar$ jar
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -n  perform Pack200 normalization after creating a new archive
    -e  specify application entry point for stand-alone application 
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -P  preserve leading '/' (absolute path) and ".." (parent directory) components from file names
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar: 
       jar cvf classes.jar Foo.class Bar.class 
Example 2: use an existing manifest file 'mymanifest' and archive all the
           files in the foo/ directory into 'classes.jar': 
       jar cvfm classes.jar mymanifest -C foo/ .

 

2)第一種方式 jar -cvf ./class2json.jar ./com  (這種方法不行)

jar -cvf ./class2json.jar ./com 
class2json.jar是jar包名稱
./com  是將這個文件打入jar包

jar包內容如下所示:

其中META-INF/MANIFEST.MF是自動生成的,如下所示:並沒有額外的jar包以及主類

Manifest-Version: 1.0
Created-By: 1.8.0_181 (Oracle Corporation)

命令行如下所示:因為我們沒指定運行的主類,所以 java -jar class2json.jar 運行失敗,因為找不到主類

 

3)第二種方式  jar cvfm jar包名稱 主類清單文件 要打包的class文件

jar cvfm class2json_1.jar ./META-INF/MANIFEST.MF com

運行jar文件:

java -jar class2json_1.jar 參數1 參數2 參數3 參數4 參數5 參數6

 這時把我們的主類清單添加到jar包里了

jar包內容如下所示:

其中META-INF/MANIFEST.MF是我們引入的,包括額外的jar包以及主類

Manifest-Version: 1.0
Class-Path: . class2json3_lib/commons-beanutils-1.8.0.jar class2json3_
lib/commons-collections-3.2.1.jar class2json3_lib/commons-lang-2.4.ja
r class2json3_lib/commons-logging-1.1.3.jar class2json3_lib/ezmorph-1
.0.6.jar class2json3_lib/json-lib-2.3-jdk15.jar
Created-By: 1.8.0_181 (Oracle Corporation)
Main-Class: com.zc.class2json.trans.test.TestDataClass2Json

命令行如下所示:指定運行的主類,所以 java -jar class2json.jar 運行成功

 

4) 用壓縮軟件進行壓縮

i)Ubuntu上,選中com以及META-INF文件夾,右鍵Compress:

因為META-INF/MANIFEST.MF指明了jar包以及主類:

ii)windows中,選中com以及META-INF文件夾,右鍵壓縮,把后綴名改為jar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM