查看jar包內容


查看jar包內容

查看jar包內容的基本命令:

jar tf jar-file

參數解釋:

  • The t option indicates that you want to view the table of contents of the JAR file.
  • The f option indicates that the JAR file whose contents are to be viewed is specified on the command line.
  • The jar-file argument is the path and name of the JAR file whose contents you want to view.

The t and f options can appear in either order, but there must not be any space between them.

This command will display the JAR file's table of contents to stdout.

You can optionally add the verbose option, v, to produce additional information about file sizes and last-modified dates in the output.

案例

jar tf TicTacToe.jar
META
-INF/MANIFEST.MF TicTacToe.class audio/ audio/beep.au audio/ding.au audio/return.au audio/yahoo1.au audio/yahoo2.au images/ images/cross.gif images/not.gif

 use the v option:

jar tvf TicTacToe.jar
    68 Thu Nov 01 20:00:40 PDT 2012 META-INF/MANIFEST.MF
   553 Mon Sep 24 21:57:48 PDT 2012 TicTacToe.class
  3708 Mon Sep 24 21:57:48 PDT 2012 TicTacToe.class
  9584 Mon Sep 24 21:57:48 PDT 2012 TicTacToe.java
     0 Mon Sep 24 21:57:48 PDT 2012 audio/
  4032 Mon Sep 24 21:57:48 PDT 2012 audio/beep.au
  2566 Mon Sep 24 21:57:48 PDT 2012 audio/ding.au
  6558 Mon Sep 24 21:57:48 PDT 2012 audio/return.au
  7834 Mon Sep 24 21:57:48 PDT 2012 audio/yahoo1.au
  7463 Mon Sep 24 21:57:48 PDT 2012 audio/yahoo2.au
   424 Mon Sep 24 21:57:48 PDT 2012 example1.html
     0 Mon Sep 24 21:57:48 PDT 2012 images/
   157 Mon Sep 24 21:57:48 PDT 2012 images/cross.gif
   158 Mon Sep 24 21:57:48 PDT 2012 images/not.gif

 

The JAR file contains the TicTacToe class file and the audio and images directory, as expected.

The output also shows that the JAR file contains a default manifest file, META-INF/MANIFEST.MF, which was automatically placed in the archive by the JAR tool.。

All pathnames are displayed with forward slashes, regardless of the platform or operating system you're using. Paths in JAR files are always relative; 

1.1對 Manifest的解釋

官方說明:

When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname

META-INF/MANIFEST.MF

When you create a JAR file, the default manifest file simply contains the following:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)

These lines show that a manifest's entries take the form of "header: value" pairs. The name of a header is separated from its value by a colon. The default manifest conforms to version 1.0 of the manifest specification and was created by the 1.7.0_06 version of the JDK.

The manifest can also contain information about the other files that are packaged in the archive. Exactly what file information should be recorded in the manifest depends on how you intend to use the JAR file. The default manifest makes no assumptions about what information it should record about other files.

Digest information is not included in the default manifest.

中文詳解:

JAR包的描述信息、啟動時的配置信息和安全性信息等均保存在META-INF下

   META-INF/MAINFEST.MF清單文件組成元素                

  META-INF/MAINFEST.MF清單文件由1個 main-section 和0到N個 individual-section 組成,而每個section中含有多個attribute組成,其中 main-section 中的attribute命名為 main-attribute ,而 individual-section 中的attribute命名為 perentry-attribute 。

  各個attribute間使用<CR><LF>作為分隔符(Unix下則使用<LF>作為分隔符,Mac下則使用<CR>作為分隔符)。

  individual-section 以名為 Name 的 perentry-attribute 來標識該區域,且作為該區域的起始行。

  示例:

Manifest-Version: 1.0
Created-By: 1.2 (Sun Microsystems Inc.)
Sealed: true
Name: foo/bar/
Sealed: false

main-section 用於描述JAR包的安全、配置信息,和對JAR包內所有包和文件的默認信息。

每個individual-section 用於描述JAR包中單個包或文件,但不是JAR包中的每個包和文件都必須配置 individual-section ,但對於需要被簽名的文件就必須配置對應的 individual-section 了。

 

1.1.1、main-attribute 詳解

   1. 常規屬性

Mainfest-Version: JAR版本號
Created-By: 生產者
Signature-Version: 簽名版本
Class-Path: 依賴項列表,若存在多個依賴項時則采用空格分隔。依賴項路徑為以JAR包路徑為參考系的相對路徑

   2. 可執行的JAR包屬性

Main-Class: main函數所在的全限定類名

 

1.1.2、 perentry-attribute 詳解

  1. Name屬性, individual-section 的起始屬性,包命名規范形如:com/test/myapp/,文件命名規范形如:com/test/myapp/MyApp.class。

  2. 定義文件內容

Content-Type: MIME類型(格式為:type/subtype。例如,image/jpeg)

 

1.1.3、注意事項                               

  1. 鍵值對獨立占據一行或多行;

  2. 每行最大長度為72個字符;

  3. 每行的最后一個字符必須以回車符換行符結尾,而且回車符換行符不能有空格(使用正則表達式表達每行規范就是/^.+\S\r\n$/);

  4. 若鍵值對獨立占據多行,那么從第二行起,必須以一個或以上的空格開頭(使用正則表達式表達第二行及其余行的規范就是/^[ ]{1,}.+\S\r\n$/)。

 


免責聲明!

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



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