查看jar包内容

时间:2024-01-04 22:28:26

查看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
Thu Nov :: PDT META-INF/MANIFEST.MF
Mon Sep :: PDT TicTacToe.class
Mon Sep :: PDT TicTacToe.class
Mon Sep :: PDT TicTacToe.java
Mon Sep :: PDT audio/
Mon Sep :: PDT audio/beep.au
Mon Sep :: PDT audio/ding.au
Mon Sep :: PDT audio/return.au
Mon Sep :: PDT audio/yahoo1.au
Mon Sep :: PDT audio/yahoo2.au
Mon Sep :: PDT example1.html
Mon Sep :: PDT images/
Mon Sep :: PDT images/cross.gif
Mon Sep :: PDT 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$/)。