1 查看JAR包內的類
另一個思路: 解壓JAR包jar -xf <jarPath>
1-1 單JAR包
-t list table of contents for archive(列出存檔內容表)
-f specify archive file name (指定存檔文件名)
[root@sdc70 ~]# jar -tf catalina.jar | grep -i "HttpHeaderSecurityFilter"
org/apache/catalina/filters/HttpHeaderSecurityFilter$XFrameOption.class
org/apache/catalina/filters/HttpHeaderSecurityFilter.class
1-2 多JAR包
-
step1 將需要查找目標類名的JAR包放在同一目錄下
-
step2 解析多JAR包的類及其路徑,存放在1個臨時文件中
find <-targetDir-> -name "*.jar" -exec jar -tf {} > ./tmpfile-multi-jar-classes.txt \;
- step3 從臨時文件中查找是否存在目標類
# cat ./tmpfile-multi-jar-classes.txt | grep -i "<keyword>"
[root@sdc70 ~]# cat ./tmpfile-multi-jar-classes.txt | grep -i "HttpHeaderSecurityFilter"
org/apache/catalina/filters/HttpHeaderSecurityFilter$XFrameOption.class
org/apache/catalina/filters/HttpHeaderSecurityFilter.class
2 JAR命令說明
2-1 JAR命令詳解
[root@sdc70 ~]# 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 (為綁定到可執行jar文件的獨立應用程序指定應用程序入口點)
-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 (為指定的jar文件生成索引信息)
-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.
(如果任何文件是一個目錄,那么它將被遞歸處理。
清單文件名、存檔文件名和入口點名稱的指定順序與'm'、'f'和'e'標志相同。)
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/ .
2-2 應用場景
case1: 解壓指定的JAR包到當前目錄
(注:JAR命令無法指定解壓的目標目錄)
jar -xf /root/test/catalina.jar
亦可利用WinRAR來實現。
case2: 運行JAR包(/Java類文件)的main方法

[方法1]java -jar ****.jar [args]
適用場景: 1)運行JAR包(需指定具體JAVA類); 2)JAR包內必須指定 META-INF/MANIFEST.MF:Main-Class
;
否則,會報此異常: no main manifest attribute, in hello-jar-1.0-SNAPSHOT.jar(hello-jar-1.0-SNAPSHOT.jar中沒有主清單屬性)
解決此異常/配置
META-INF/MANIFEST.MF:Main-Class
的方式:
- [#1 事后型] 直接編輯已有JAR包進行配置。利用WinRAR解壓 JAR包;在其 META-INF/MANIFEST.MF 文件內配置 Main-Class;再利用WinRAR壓縮為zip壓縮包,重命名為jar即可
- [#2 事前型] 普通Maven項目: pom.xml中
<build>
內配置maven的插件(maven-assembly-plugin
),指定 mainClass屬性值- [#3 事前型] SpringBoot的Maven項目: pom.xml中
<build>
內配置maven的插件(spring-boot-maven-plugin
)即可Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: cn.johnnyzen.myBootApplication
java -jar myApp.jar arg_aaa arg_bbb
[方法2]java -classpath ****.jar ****.****.className [args]
適用場景: 1)運行JAR包(無需指定具體JAVA類); 2)JAR包內無需指定 META-INF/MANIFEST.MF:Main-Class
如果沒有在JAR包內的META-INF/MANIFEST.MF
中指定Main-Class
配置時;
或者
如果JAR包中有多個main方法,運行時需指定某個特定的main方法
java -cp myApp.jar com.smbea.dubbo.bin.Console aaa bbb
-cp := -classpath
[方法3]java -Djava.ext.dirs=<多個jar包的目錄> com.test.HelloWordMain
(如果用-cp
,則需要寫每個jar,很麻煩)
【風險】(方法3)
從結果來看,兩者(-cp
/ -Djava.ext.dirs
)的區別在於:-Djava.ext.dirs
會覆蓋JAVA本身的ext
設置。
一般情況下程序運行並無差異,因為可以看到ext目錄下也就幾個jar;
但是如果使用java.security
相關類時,就會發現-Djava.ext.dirs
會導致library
找不到進而報錯。
(解決辦法暫時想到2種:)
- 將相關lib復制到新的ext director。驗證可行
- 配置多個director。可以使用冒號分隔(-Djava.ext.dirs=directoryA:directoryB)
case3 : 生成JAR壓縮包(打包)
-
方式1 利用WinRAR對class文件 打JAR包
(詳見本文第4章 WinRAR部分) -
方式2
jar cvf ./myJarName.jar ./
將當前目錄下的所有文件打成jar包,jar包名為main.jar(放在工作目錄下)
3 反編譯: file.class → file.java
java 反編譯 := java decomplier
- 需求來源
[case1]
當項目已部署時,不確定已部署的war/jar包內某個類文件中是否是預期的內容。
此時,最低成本的做法或許是:直接反編譯看看~
一般,Java Web項目中,class文件在Tomcat的WEB應用此路徑下: C:xx/wepapps/xxxApp/WEB-INF/classes/.../Example.class
- 前置條件
- 反編譯工具 jad.exe
可到該博文處下載: Java 反編譯工具幾枚(class轉java) - CSDN
亦可下載博主提供的: jad158g.win.zip - zip包 解壓即安裝 (安裝包來源於:https://varaneckas.com/jad/)
- 反編譯
step1 切換到jad.exe的目錄下
step2 jad -o -r -s java -d src xx/wepapps/xxxApp/WEB-INF/classes/.../Example.class
(執行完畢后,jad的當前目錄下會生成1個名為src的子目錄,其內會存在反編譯后的java文件: Example.java)

- jad 命令解析
-o:覆蓋舊文件,而且不用提示確認。
-r:重新加載生成包結構。
-s (java):定義輸出文件的擴展名。jad為默認擴展名,咱們反編譯后,當然是要.java源文件了。
-d:輸出文件的目錄。src表示反編譯后的所有文件都放在src目錄下。
4 使用WinRAR: 查看/修改/刪除| 解壓/生成 JAR壓縮包
- 使用WinRAR 解壓/生成 JAR 壓縮包
[解壓]
step1 選中目標JAR包,右鍵
step2 (選擇WinRAR)解壓到當前目錄
[生成/壓縮]
step1 選中待壓縮的目錄(一般含:com / META-INF 2個目錄),右鍵
step2 (選擇WinRAR)"添加到壓縮文件",選擇壓縮格式為"zip"【必須是zip格式】
step3 重命名zip后綴為jar,即可
- 使用WinRAR 查看/修改JAR包
step1 打開WinRAR軟件
step2 WinRAR>打開壓縮文件>(選中目標JAR包)>(查看/修改/刪除)
5 反編譯神器: Java Decomplier-GUI
- 下載地址 - Github: http://java-decompiler.github.io/

