Linux|Java|jar包的解压和重新打包(更新配置)

前言:

最近有遇到一个关于jar包的坑,差点没上去,感觉还是有点意思的就记录一下。

一,

前情

有一个小项目,该项目是springboot开发的,有连接数据库,数据库使用的是postgresql。

部署没什么好说的,肯定先Java -jar jar包 前台跑一下项目,然后根据报错调试。

OK,根据前台打印的日志,发现是数据库连接IP写错了,这个没什么好说的,找一个空目录,解压,修改关于数据库的配置,在重新打包成jar包

可是。。。。但可是。。。。。jar包直接报错了,大概就是这样的报错(随便找了一个jar包演示)

[root@node1 media]# java -jar ojdbc14.jar 
no main manifest attribute, in ojdbc14.jar

OK,解压和重新打包的命令是这样的,清单文件没有动过丫,很奇怪:

jar -xf jar包名称
jar cvf  jar包名称 *

二,

问题排查

还是以这个ojdbc的jar包为例,观察打包前后它的MANIFEST.MF文件的内容

打包前,也就是解压后:

[root@node1 META-INF]# cat MANIFEST.MF 
Manifest-Version: 1.0
Specification-Title:    Oracle JDBC driver classes for use with JDK14
Sealed: true
Created-By: 1.4.2_08 (Sun Microsystems Inc.)
Implementation-Title:   ojdbc14.jar
Specification-Vendor:   Oracle Corporation
Specification-Version:  Oracle JDBC Driver version - "10.2.0.1.0XE"
Implementation-Version: Oracle JDBC Driver version - "10.2.0.1.0XE"
Implementation-Vendor:  Oracle Corporation
Implementation-Time:    Wed Jan 25 01:28:31 2006

Name: oracle/sql/converter/
Sealed: false

Name: oracle/sql/
Sealed: false

Name: oracle/sql/converter_xcharset/
Sealed: false

打包后:

Manifest-Version: 1.0
Created-By: 1.8.0_271 (Oracle Corporation)

OK,这说明打包的方式是不对的,漏掉了某些参数,导致MANIFEST.MF文件的改变,进而导致包无法使用

三,

解决方案

正确的打包姿势:

先看看jar命令的帮助:

[root@node1 media]# jar --help
Illegal option: -
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.

OK,-M参数特意提到不修改MANIFEST.MF,还一个关键参数-0  不使用压缩(压缩操作有时候会导致不可预测的问题,说人话就是压缩不稳定)

那么,正确的打包命令就是:

jar cvM0f jar包名 *

更换打包姿势后,jar包正确运行,发现配置生效,指向了正确的数据库,一切就很完美了~~~~!!!!

小结:

有时候还是不够认真,其实先看看命令帮助也就不会掉坑里了。

猜你喜欢

转载自blog.csdn.net/alwaysbefine/article/details/131864301
今日推荐