jar-The Java Archive Tool

本文介绍了Java归档工具jar。阐述了jar工具的三种输入文件,即清单文件(可选)、目标jar文件和要归档的文件。还说明了若有预先存在的清单文件,可用 -m 选项指定。添加文件到jar归档时会存储文件及其哈希值,清单文件采用RFC822 ASCII格式,便于查看处理。

jar-The Java Archive Tool

Combines multiple files into a single JAR archive file.

SYNOPSIS

jar [ options ] [manifest] destination input-file [input-files]

DESCRIPTION

The jar tool is a java application that combines multiple files into a single JAR archive file. jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. However, jar was designed mainly to facilitate the packaging of java applets or applications into a single archive. When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they may be downloaded by a java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. <!-- REMIND: put a link to download benchmark statistics in here --> jar also compresses files and so further improves download time. In addition, it allows individual entries in a file to be signed by the applet author so that their origin can be authenticated. The syntax for the jar tool is almost identical to the syntax for the tar command.

The 3 types of input files for the jar tool are

  • manifest file (optional)
  • destination jar file
  • files to be archived

Typical usage is

	% jar cf myjarfile *.class 
In this example, all the class files in the current directory are placed into the file named "myjarfile". A manifest file is automatically generated by the jar tool and is always the first entry in the jar file. By default, it is named META-INF/MANIFEST.INF. The manifest file is the place where any meta-information about the archive is stored. Refer to the manifest specification for details about how meta-information is stored in the manifest file.

If you have a pre-existing manifest file that you want the jar tool to use for the new jar archive, you can specify it using the -m option:

	% jar cmf myManifestFile myJarFile *.class

When files are added to a jar archive, the file and its MD5 and SHA hashes are stored. The hashes are entered into the manifest file. It is easy to view and process the contents of the manifest file, since it uses RFC822 ascii format.

OPTIONS

c
Creates a new or empty archive on the standard output.

t
Lists the table of contents from standard output.

x file
Extracts all files, or just the named files, from standard input. If file is omitted, then all files are extracted; otherwise, only the specified file or files are extracted.

f
The second argument specifies a jar file to process. In the case of creation, this refers to the name of the jar file to be created (instead of on stdout). For table or xtract, the second argument identifies the jar file to be listed or extracted.

v
Generates verbose output on stderr.

m
Includes manifest information from specified pre-existing manifest file. Example use:
jar cmf myManifestFile myJarFile *.class
You can add special-purpose name-value attribute headers to the manifest file that aren't contained in the default manifest. Examples of such headers would be those for vendor information, version information, and package sealing. See the JAR Files trail in the Java Tutorial for examples of using the m option.

0
Store only, without using ZIP compression.

M
Do not create a manifest file for the entries.

If any of "files" is a directory, then that directory is processed recursively.

EXAMPLES

To add all the files in a particular directory to an archive:

C:/WWWROOT/JAVA> ls
12/09/96  12:20a        <DIR>          .    
12/09/96  12:17a        <DIR>          ..  
12/09/96  12:18a                 1,010 0.au
12/09/96  12:18a                   946 1.au
12/09/96  12:18a                 1,039 2.au
12/09/96  12:18a                   993 3.au
12/09/96  12:18a                 1,006 4.au
12/09/96  12:18a                 1,016 5.au
12/09/96  12:18a                 1,048 6.au
12/09/96  12:18a                   980 7.au
12/09/96  12:18a                 1,064 8.au
12/09/96  12:18a                   989 9.au  
12/09/96  12:19a                48,072 spacemusic.au
12/09/96  12:19a                   527 at_work.gif
12/09/96  12:19a                12,818 monkey.jpg
12/09/96  12:19a                16,242 Animator.class
12/09/96  12:20a                 3,368 Wave.class
              17 File(s)         91,118 bytes
                            414,983,168 bytes free

C:/WWWROOT/JAVA> jar cvf bundle.jar *
adding: 0.au
adding: 1.au
adding: 2.au
adding: 3.au
adding: 4.au
adding: 5.au
adding: 6.au
adding: 7.au
adding: 8.au
adding: 9.au
adding: Animator.class
adding: Wave.class
adding: at_work.gif
adding: monkey.jpg
adding: spacemusic.au
C:/WWWROOT/JAVA>
If already have subdirectories for images, audio files and classes in my html directory, I might jar up each directory into a single jar file:
C:/WWWROOT/JAVA> DIR
12/09/96  12:11a        <DIR>          .
12/09/96  12:17a        <DIR>          ..
12/03/96  06:54p        <DIR>          audio
12/06/96  02:02p        <DIR>          images
12/09/96  12:10a        <DIR>          classes
               6 File(s)        207,360 bytes
                            414,983,168 bytes free

C:/WWWROOT/JAVA> jar cvf bundle.jar audio classes images
adding: audio/1.au
adding: audio/2.au
adding: audio/3.au
adding: audio/spacemusic.au
adding: classes/Animator.class
adding: classes/Wave.class
adding: images/monkey.jpg
adding: images/at_work.gif
C:/WWWROOT/JAVA/Animator> dir
12/09/96  12:11a        <DIR>          .
12/09/96  12:17a        <DIR>          ..
12/09/96  12:11a               207,360 bundle.tar
12/03/96  06:54p        <DIR>          audio
12/06/96  02:02p        <DIR>          images
12/09/96  12:10a        <DIR>          classes
               6 File(s)        207,360 bytes
                            414,983,168 bytes free

C:/WWWROOT/JAVA/Animator> 
I can then see the entry names in the jarfile using the jar tool and the "t" option:
C:/WWWROOT/JAVA/Animator> jar tf bundle.jar
META-INF/MANIFEST.MF
audio/1.au
audio/2.au
audio/3.au
audio/spacemusic.au
classes/Animator.class
classes/Wave.class
images/monkey.jpg
images/at_work.gif
C:/WWWROOT/JAVA/Animator> 
Enumerating verbosely (with the "v" option) will tell me more information about the files in the archive, such as their size and last modified date:
C:/WWWROOT/JAVA/Animator> jar tvf bundle.jar
   145 Thu Aug 01 22:27:00 PDT 1996 META-INF/MANIFEST.MF
   946 Thu Aug 01 22:24:22 PDT 1996 audio/1.au
  1039 Thu Aug 01 22:24:22 PDT 1996 audio/2.au
   993 Thu Aug 01 22:24:22 PDT 1996 audio/3.au
 48072 Thu Aug 01 22:24:23 PDT 1996 audio/spacemusic.au
 16711 Thu Aug 01 22:25:50 PDT 1996 classes/Animator.class
  3368 Thu Aug 01 22:26:02 PDT 1996 classes/Wave.class
 12809 Thu Aug 01 22:24:48 PDT 1996 images/monkey.jpg
   527 Thu Aug 01 22:25:20 PDT 1996 images/at_work.gif
C:/WWWROOT/JAVA/Animator>
Building dependency tree... Done Reading state information... Done libnss3-tools is already the newest version (2:3.98-1build1). The following package was automatically installed and is no longer required: libllvm19 Use 'sudo apt autoremove' to remove it. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. dministrator@DESKTOP-EC720EK:~$ # Linux安装JNA依赖 apt insdministrator@DESKTOP-EC720EK:~$ sudo apt install libjna-java 案 choco install jna # 需安装Chocolatey包管理器 # Windows解决方案 choco install jna # 需安装Chocolatey包管理器 Reading package lists... Done Building dependency tree... Done Reading state information... Done The following package was automatically installed and is no longer required: libllvm19 Use 'sudo apt autoremove' to remove it. The following additional packages will be installed: libjna-jni The following NEW packages will be installed: libjna-java libjna-jni 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 234 kB of archives. After this operation, 417 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu noble/universe amd64 libjna-jni amd64 5.14.0-1 [33.9 kB] Get:2 http://archive.ubuntu.com/ubuntu noble/universe amd64 libjna-java all 5.14.0-1 [201 kB] Fetched 234 kB in 3s (82.5 kB/s) Selecting previously unselected package libjna-jni. (Reading database ... 88405 files and directories currently installed.) Preparing to unpack .../libjna-jni_5.14.0-1_amd64.deb ... Unpacking libjna-jni (5.14.0-1) ... Selecting previously unselected package libjna-java. Preparing to unpack .../libjna-java_5.14.0-1_all.deb ... Unpacking libjna-java (5.14.0-1) ... Setting up libjna-jni (5.14.0-1) ... Setting up libjna-java (5.14.0-1) ... dministrator@DESKTOP-EC720EK:~$ # 重建JAR索引文件 .jar dministrator@DESKTOP-EC720EK:~$ jar -i stegsolve.jar The JAR index (META-INF/INDEX.LIST) is ignored at run-time since JDK 18 Warning: The --generate-index/-i option is deprecated, and may be ignored or removed in a future release java.nio.file.NoSuchFileException: stegsolve.jar at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92) at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106) at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) at java.base/sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55) at java.base/sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:171) at java.base/sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99) at java.base/java.nio.file.Files.readAttributes(Files.java:1854) at java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:1445) at java.base/java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:724) at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:251) at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:180) at java.base/java.util.jar.JarFile.<init>(JarFile.java:345) at java.base/java.util.jar.JarFile.<init>(JarFile.java:316) at java.base/java.util.jar.JarFile.<init>(JarFile.java:255) at jdk.jartool/sun.tools.jar.Main.getJarPath(Main.java:1570) at jdk.jartool/sun.tools.jar.Main.genIndex(Main.java:1599) at jdk.jartool/sun.tools.jar.Main.run(Main.java:402) at jdk.jartool/sun.tools.jar.Main.main(Main.java:1700)好了吗
最新发布
11-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值