jar,用于操作JAR(Java Archive)文件。
命令行语法格式
# 创建JAR文件(c) jar c[efmMnv0] [entrypoint] [jarfile] [manifest] [-C dir] file ... [-Joption ...] [@arg-file ...] # 更新JAR文件(u) jar u[efmMnv0] [entrypoint] [jarfile] [manifest] [-C dir] file ... [-Joption ...] [@arg-file ...] # 提取JAR文件(x) jar x[vf] [jarfile] file ... [-Joption ...] [@arg-file ...] # 列出JAR文件的内容(t) jar t[vf] [jarfile] file ... [-Joption ...] [@arg-file ...] # 向JAR文件中添加索引(i) jar i jarfile [-Joption ...] [@arg-file ...]
命令简述(DESCRIPTION)
jar命令是通用存档和压缩工具,基于ZIP和ZLIB压缩格式。
但是jar命令主要还是用于将Java应用程序打包到单一的存档中,同时jar命令还压缩文件,极大节省下载时间和空间。
jar命令还允许文件中的单个条目由小应用程序作者签名,以便它们的来源可以被认证。
JAR文件可以用作类路径条目,无论是否被压缩。
jar命令的语法类似于tar命令的语法。它有几种操作模式,由一个必须指定的操作参数(c、u、x等等)来定义。其他的参数是:用于修改操作行为的选项;或执行操作时所需的操作数。
命令使用中需要注意的问题
选项的顺序和操作数的顺序要对应:
而
操作参数及其含义(OPERATION ARGUMENTS)
使用jar命令时,必须通过指定以下操作参数之一来选择要执行的操作。可以将它们与命令行上的其他单字母选项进行混合,但通常操作参数是指定的第一个参数。
c: 创建一个新的JAR文件。
i: 为JAR文件生成索引信息。
t: 列出JAR文件的内容。
u: 更新一个JAE文件。
x: 提取JAR文件。
选项及其含义(OPTIONS)
使用以下选项自定义如何创建、更新、提取、查看JAR文件:
-e, e
将由「entrypoint操作数指定的类」设置为绑定到可执行JAR文件中的独立Java应用程序的入口点。使用此选项创建或覆盖manifest文件中的Main-Class属性值。
当创建(c)或更新(u)JAR文件时,可以使用e选项。
例如,以下命令使用Main.class文件创建Main.jar,其中清单中的Main-Class属性值设置为Main:
JRE中运行该JAR包:
如果入口点类名在包中,则可以使用点(.)或斜杠(/)作为分隔符。例如,如果Main.class在一个名为mydir的包中,则可以通过以下方式之一指定入口点:
jar -cfe Main.jar mydir.Main mydir/Main.class
注意:当特定manifest也包含Main-Class属性时,并同时指定m和e选项,会导致一个模糊的Main-Class的错误,并且终止jar命令创建或更新操作。
-f, f
将由jarfile指定的文件设置为要创建(c)、更新(u)、提取(x)、查看(t)的JAR文件的名称。
省略f选项和jarfile操作数指示,jar命令从stdin(对于x和t)读取JAR文件名,或者将JAR文件发送到stdout(对于c和u)。
-m, m
包括来自由jar命令的manifest文件(位于META-INF/MANIFEST.MF的存档中)的manifest指定的文件中的属性的名称和值。
jar命令将属性的名称和值添加到JAR文件中,如果相同的属性名存在,则jar命令会更新属性的值。
当创建(c)或更新(u)JAR文件时,可以使用m选项。
您可以向默认manifest文件中不包含的清单添加特殊用途的名值属性对。例如,可以添加指定供应商信息,发行信息,打包密封或使JAR捆绑应用程序可执行的属性。
有关的使用案例查看:http://docs.oracle.com/javase/tutorial/deployment/jar/index.html
-M, M
不创建manifest文件条目(对于c和u),或者当存在(对于u))删除manifest文件条目。
创建(c)或更新(u)JAR文件时,可以使用M选项。
-n, n
创建(c)JAR文件时,此选项将正规化Archive,以使内容不受pack200(1)命令的打包和解包操作的影响。
没有这种正规化,签名的JAR的签名可能变得无效。
v
生成详细输出到标准输出。参见下面的示例。
–0, 0
(零)创建(c)或更新(u)JAR文件而不使用ZIP压缩。
-C dir
当创建(c)或更新(u)JAR文件时,此选项会在处理由file操作数指定的文件时临时,更改目录。
其操作类似于UNIX tar的-C选项。例如,以下命令更改为classes目录,并将该目录中的Bar.class文件添加到my.jar中:
以下命令更改为classes目录,并添加到classes目录中的所有文件(不创建JAR文件中的classes目录)的my.jar中,然后更改回原始目录,然后再更改为bin目录以添加Xyz.class到my.jar
如果类包含文件bar1和bar2,则运行上一个命令后,JAR文件将包含以下内容:
META-INF/
META-INF/MANIFEST.MF
bar1
bar2
Xyz.class
-Joption
设置JRE运行JAR文件时,使用指定的JVM选项。 java(1)命令的手册中介绍了JVM选项。例如,-J-Xms48m将启动内存设置为48MB。
操作数及其含义(OPERANDS)
The following operands are recognized by the jar command.
file
当创建(c)或更新(u)JAR文件时,file定义了应添加到Archive中的文件或目录的路径和名称。
当提取(x)或查看(t)JAR文件时,file定义要提取或列出的文件的路径和名称。
必须至少指定一个有效的文件或目录。多个file用空格分隔。如果使用entrypoint,jarfile或manifest操作数,那么file必须在它们之后指定。
entrypoint
当创建(c)或更新(u)JAR文件时,entrypoint定义了类的名称,应该是捆绑到可执行JAR文件中的独立Java应用程序的入口点的。
如果存在e选项,则必须指定entrypoint。
jarfile
定义要创建(c)、更新(u)、提取(x)、查看(t)的JAR文件的名称。
如果f选项存在,则必须指定jarfile。
省略f选项和jarfile,表示jar命令从stdin(对于x和t)接受JAR文件名,或者将JAR文件发送到stdout(对于c和u)。
当索引(i)JAR文件时,指定jarfile时,不使用f选项。
manifest
在创建(c)或更新(u)JAR文件时,manifest定义要包含在JAR文件中的MANIFEST.MF中的属性的名称和值的预先存在的清单文件。
如果存在f选项,则必须指定清单操作数。
@arg-file
to the jar command with the at sign (@) as a prefix. When the jar command encounters an argument
beginning with the at sign, it expands the contents of that file into the argument list.
they are passed to the launcher, which does not support argument files). The arguments within a file
can be separated by spaces or newline characters. File names within an argument file are relative to
the current directory from which you run the jar command, not relative to the location of the argument
file. Wild cards, such as the asterisk (*), that might otherwise be expanded by the operating system
shell, are not expanded.
directory output by the find command:
relative paths are relative to the current working directory of the jar command, not to the path
passed in, for example:
注意事项(NOTES)
The e, f, and m options must appear in the same order on the command line as the entrypoint, jarfile, and
manifest operands, for example:
jar cmef myManifestFile MyMainClass myFile.jar *.class
简单示例(EXAMPLES)
Example 1 Adding All Files From the Current Directory With Verbose Output
% ls
1.au Animator.class monkey.jpg
2.au Wave.class spacemusic.au
3.au at_work.gif
% jar cvf bundle.jar *
added manifest
adding: 1.au(in = 2324) (out= 67)(deflated 97%)
adding: 2.au(in = 6970) (out= 90)(deflated 98%)
adding: 3.au(in = 11616) (out= 108)(deflated 99%)
adding: Animator.class(in = 2266) (out= 66)(deflated 97%)
adding: Wave.class(in = 3778) (out= 81)(deflated 97%)
adding: at_work.gif(in = 6621) (out= 89)(deflated 98%)
adding: monkey.jpg(in = 7667) (out= 91)(deflated 98%)
adding: spacemusic.au(in = 3079) (out= 73)(deflated 97%)
Example 2 Adding Files From Subdirectories
% ls -F
audio/ classes/ images/
% jar cvf bundle.jar audio classes images
added manifest
adding: audio/(in = 0) (out= 0)(stored 0%)
adding: audio/1.au(in = 2324) (out= 67)(deflated 97%)
adding: audio/2.au(in = 6970) (out= 90)(deflated 98%)
adding: audio/3.au(in = 11616) (out= 108)(deflated 99%)
adding: audio/spacemusic.au(in = 3079) (out= 73)(deflated 97%)
adding: classes/(in = 0) (out= 0)(stored 0%)
adding: classes/Animator.class(in = 2266) (out= 66)(deflated 97%)
adding: classes/Wave.class(in = 3778) (out= 81)(deflated 97%)
adding: images/(in = 0) (out= 0)(stored 0%)
adding: images/monkey.jpg(in = 7667) (out= 91)(deflated 98%)
adding: images/at_work.gif(in = 6621) (out= 89)(deflated 98%)
% ls -F
audio/ bundle.jar classes/ images/
Example 3 Listing the Contents of JAR
% jar tf bundle.jar
META-INF/
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
Example 4 Adding an Index
Use the i option when you split the interdependent classes for a stock trade application into three JAR
files: main.jar, buy.jar, and sell.jar. If you specify the Class-Path attribute in the main.jar manifest,
then you can use the i option to speed up the class loading time for your application:
Class-Path: buy.jar sell.jar
jar i main.jar
An INDEX.LIST file is inserted to the META-INF directory. This enables the application class loader to
download the specified JAR files when it is searching for classes or resources.
The application class loader uses the information stored in this file for efficient class loading. To copy
directories, first compress files in dir1 to stdout, then pipeline and extract from stdin to dir2 (omitting
the -f option from both jar commands):
(cd dir1; jar c .) | (cd dir2; jar x)
相关文档(SEE ALSO)
pack200(1)
参考文献
- http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jar.html#BGBFFFGG
- man 1 jar, version OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-3-b15)