[Detailed explanation of the usage of the jar command]

JAR package is a unique compressed file in Java, in fact, everyone can understand it as a .zip package. Of course, there are differences. There is a META-INF\MANIFEST.MF file in the JAR package. When you find a JAR package, it will be automatically generated.

The JAR package is generated by the JDK installation directory \bin\jar.exe command. When we install the JDK and set the path, we can use the jar.exe command normally. It will use the classes in the lib\tool.jar toolkit. . Leave those details alone.

Let's see how it is used:
 
1. jar command parameters:
 
jar command format: jar {ctxuf }[ vme 0 M i ][-C directory] file name...
 
Among them, the four parameters {ctxu} must be selected Choose one. [vfme 0 M i ] is an optional parameter, and the file name is also required.
 
-c create a jar package
-t display the list of contents in the
jar -x decompress the jar package
-u add files to the jar package
-f specify the file name of the jar package
-v generate a detailed report and output it to the standard device
-m Specify the manifest.mf file. (The jar package and its contents can be set in the manifest.mf file)
-0 Do not compress the contents when generating the jar package
-M Do not generate a manifest file of all files (Manifest. mf). This parameter is the same as the setting of ignoring the -m parameter
-i creates an index file for the specified jar file
-C means go to the corresponding directory to execute the jar command, which is equivalent to cd to that directory, and then execute the jar command without -C
 
. Jar usage example:
 
(1) Create a jar package

1
jar cf hello.jar hello

Use the test directory to generate the hello.jar package. If hello.jar exists, override
 
(2) to create and display the packaging process

1
jar cvf hello.jar hello

Use the hello directory to create the hello.jar package, and display the creation process
example:

1
E:\>jar cvf hello.jar hello

Manifest
added: hello/(read in = 0) (write out = 0) (stored 0%)
Add: hello/TestServlet2.class (read in = 1497) (write out = 818) (compressed 45 %)
Added: hello/HelloServlet.class (read in = 1344) (write out = 736) (compressed by 45%)
Added: hello/TestServlet1.class (read in = 2037) (write out = 1118) (compressed by 45 %)
 
(3) Display the jar package:

jar tvf hello.jar View the contents of the hello.jar package The
specified jar package must exist, otherwise a FileNoutFoundException will occur.
 
(4) Unzip the jar package:

1
jar xvf hello.jar

Unzip hello.jar to the current directory
 
(5) Add files to the jar:

1
jar uf hello.jar HelloWorld.java

Add HelloWorld.java to the hello.jar package
 
(6) to create an uncompressed content jar package:

1
jar cvf0 hello.jar *. class

Use all .class files in the current directory to generate an uncompressed jar package
 
(7) to create a jar package with manifest.mf file:

1
jar cvfm hello.jar manifest.mf hello

The created jar package has one more META-INF directory, and one more manifest.mf file is recorded in META-INF. As for the role of manifest.mf, it will be mentioned later.
 
(8) Ignore the manifest.mf file:

1
jar cvfM hello.jar hello

生成的jar包中不包括META-INF目录及manifest.mf文件
 
(9)加-C应用:

1
jar cvfm hello.jar mymanifest.mf -C hello/

表示在切换到hello目录下然后再执行jar命令
 
(10)-i为jar文件生成索引列表:

当一个jar包中的内容很好的时候,你可以给它生成一个索引文件,这样看起来很省事。

1
jar i hello.jar

执行完这条命令后,它会在hello.jar包的META-INF文件夹下生成一个名为INDEX.LIST的索引文件,它会生成一个列表,最上边为jar包名。
 
(11)导出解压列表:

jar tvf hello.jar >hello.txt   如果你想查看解压一个jar的详细过程,而这个jar包又很大,屏幕信息会一闪而过,这时你可以把列表输出到一个文件中,慢慢欣赏!
 
 (12)jar -cvf hello.jar hello/*

   例如原目录结构如下:
   hello
     |---com
     |---org
 
你本想只把com目录和org目录打包,而这时jar命令会连同hello目洋也一块打包进。这点大家要注意。jar命令生成的压缩文件会包含它后边出的目录。我们应该进入到hello目录再执行jar命令。
 
注意:manifest.mf这个文件名,用户可以任指定,但jar命令只认识Manifest.mf,它会对用户指定的文件名进行相应在的转换,这不需用户担心。
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326187180&siteId=291194637