1.帮助文档
[root@node1 java]# jar
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-n perform Pack200 normalization after creating a new archive
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-P preserve leading '/' (absolute path) and ".." (parent directory) components from file names
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.
Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .
1.1 案例演示
Example 1:将两个class文件归档到名为classes.jar的归档文档中
#切换到testjar目录
cd /opt/testjar
#创建连个class文件
touch Foo.class Bar.class
jar -cvf classes.jar Foo.class Bar.class
Example 2: 使用现有的清单文件’mymanifest’并将/foo目录中的所有文件存档到“classes.jar”中
#创建一个空的清单文件
touch mymanifest
#创建foo文件夹
mkdir foo
#将文件存放到foo目录下
#执行归档操作
jar cvfm classes.jar mymanifest -C foo/ .
1.2 列出jar包中的文件清单
jar -tf classes.jar
1.3 添加新文件到指定jar包中
创建a.txt文件,文件内容为‘寻找手艺人’
[root@node1 testjar]# cat>>a.txt<<EOF
> 寻找手艺人
> EOF
将a.txt更新到classes.jar中
jar -uf classes.jar a.txt
1.4 提取出内部jar包的指定文件
从classes.jar中提取a.txt文件
jar -xf classes.jar a.txt
提取后修改a.txt文件内容
vim a.txt
1.5 更新修改后的文件到内部jar包.(存在覆盖,不存在就新增)
jar -uf classes.jar a.txt
1.6 更新内部jar包到jar文件
把mysql-connector-jar.jar归档到classes.jar归档文件中
jar -uf classes.jar mysql-connector-java.jar
或者通过vim查看是否已更改
vim classes.jar
输出以下内容:
" zip.vim version v27
" Browsing zipfile /opt/testjar/classes.jar
" Select a file with cursor and press ENTER
META-INF/
Foo.class
Bar.class
mysql-connector-java.jar
2.解压jar包,修改后重新打包jar
解压classes.jar到/aaa目录下
mkdir aaa
unzip classes.jar -d ./aaa
在/aaa目录中更新a.txt内容,并重新打包为jar文件
jar -cfM0 new-classes.jar ./*