linux 解压修改jar包内容并重新打包jar

1 篇文章 0 订阅

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 ./*

在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值