Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time

馃悓 Java鐩稿叧鑴氭湰

鉂楋笍 master鏄 涓嶅啀寮鍙戠殑鑰佺増鏈1.x鐨勫垎鏀 鉂楋笍

璇峰垏鍒 鏂扮増鏈殑寮鍙戝垎鏀



鍏充簬Java鎺掗敊涓庤瘖鏂紝鍔涜崘锔Arthas 鉂わ笍

Arthas鍔熻兘寮傚父(馃槣)寮哄姴锛屼笖鍦ㄩ樋閲屽反宸寸嚎涓婃敮鎸佷娇鐢ㄥ骞淬傛垜鑷繁涔熷父鐢紝涓瀹氳鐪嬬湅鐢ㄧ敤锛

Arthas鏄氳繃Agent鏂瑰紡鏉ヨ繛鎺ヨ繍琛岀殑Java杩涚▼銆佷富瑕侀氳繃浜や簰寮忔潵瀹屾垚鍔熻兘锛屼笌涔嬪搴旂殑鑴氭湰鏂瑰紡涔熸湁鍏朵紭鍔匡紝濡傦細

  1. 鍙互鍦ㄨ繘绋嬩笉鑳藉惎鍔ㄧ殑鎯呭喌涓嬪畬鎴愯瘖鏂紙濡備緷璧栦腑鐨勯噸澶嶇被鍒嗘瀽銆ClassPath涓婄殑璧勬簮鎴栫被鏌ユ壘锛
  2. 寮閿灏戯紱绠鍗曞皯渚濊禆锛堝氨绾枃鏈殑涓涓剼鏈枃浠讹級
  3. 鏂逛究涓庯紙宸叉湁鐨勶級宸ュ叿锛堝awksedcron锛夈佹祦绋嬫垨璁炬柦闆嗘垚锛岃繘涓姝ョ紪绋/鑷姩鍖

璇锋寜闇鎸夊満鏅夌敤銆


馃嵑 show-busy-java-threads

鉂楋笍 master鏄 涓嶅啀寮鍙戠殑鑰佺増鏈1.x鐨勫垎鏀 鉂楋笍

璇峰垏鍒 鏂扮増鏈殑寮鍙戝垎鏀


鐢ㄤ簬蹇熸帓鏌JavaCPU鎬ц兘闂(top us鍊艰繃楂)锛岃嚜鍔ㄦ煡鍑鸿繍琛岀殑Java杩涚▼涓秷鑰CPU澶氱殑绾跨▼锛屽苟鎵撳嵃鍑哄叾绾跨▼鏍堬紝浠庤岀‘瀹氬鑷存ц兘闂鐨勬柟娉曡皟鐢ㄣ
鐩墠鍙敮鎸Linux銆傚師鍥犳槸MacWindowsps鍛戒护涓嶆敮鎸佸垪鍑鸿繘绋嬬殑绾跨▼id锛屾洿澶氫俊鎭弬瑙#33锛屾杩庢彁渚涜В娉曘

PS锛屽浣曟搷浣滃彲浠ュ弬瑙@bluedavy銆婂垎甯冨紡Java搴旂敤銆鐨勩5.1.1 CPU娑堣楀垎鏋愩戜竴鑺傦紝璇村緱寰堣缁嗭細

  1. top鍛戒护鎵惧嚭娑堣CPU楂樼殑Java杩涚▼鍙婂叾绾跨▼id
    1. 寮鍚嚎绋嬫樉绀烘ā寮忥紙top -H锛屾垨鏄墦寮top鍚庢寜H
    2. CPU浣跨敤鐜囨帓搴忥紙top缂虹渷鏄寜CPU浣跨敤闄嶅簭锛屽凡缁忓悎瑕佹眰锛涙墦寮top鍚庢寜P鍙互鏄惧紡鎸囧畾鎸CPU浣跨敤闄嶅簭锛
    3. 璁颁笅Java杩涚▼id鍙婂叾CPU楂樼殑绾跨▼id
  2. 鏌ョ湅娑堣CPU楂樼殑绾跨▼鏍堬細
    1. 鐢ㄨ繘绋id浣滀负鍙傛暟锛jstack鍑烘湁闂鐨Java杩涚▼
    2. 鎵嬪姩杞崲绾跨▼id鎴愬崄鍏繘鍒讹紙鍙互鐢printf %x 1234
    3. jstack杈撳嚭涓煡鎵惧崄鍏繘鍒剁殑绾跨▼id锛堝彲浠ョ敤vim鐨勬煡鎵惧姛鑳/0x1234锛屾垨鏄grep 0x1234 -A 20
  3. 鏌ョ湅瀵瑰簲鐨勭嚎绋嬫爤锛屽垎鏋愰棶棰

鏌ラ棶棰樻椂锛屼細瑕佸娆′笂闈㈢殑鎿嶄綔浠ュ垎鏋愮‘瀹氶棶棰橈紝杩欎釜杩囩▼澶箒鐞愬お鎱簡

鐢ㄦ硶

show-busy-java-threads
# 浠庢墍鏈夎繍琛岀殑Java杩涚▼涓壘鍑烘渶娑堣桟PU鐨勭嚎绋嬶紙缂虹渷5涓級锛屾墦鍗板嚭鍏剁嚎绋嬫爤

# 缂虹渷浼氳嚜鍔ㄤ粠鎵鏈夌殑Java杩涚▼涓壘鍑烘渶娑堣桟PU鐨勭嚎绋嬶紝杩欐牱鐢ㄦ洿鏂逛究
# 褰撶劧浣犲彲浠ユ墜鍔ㄦ寚瀹氳鍒嗘瀽鐨凧ava杩涚▼Id锛屼互淇濊瘉鍙細鏄剧ず浣犲叧蹇冪殑閭d釜Java杩涚▼鐨勪俊鎭
show-busy-java-threads -p <鎸囧畾鐨凧ava杩涚▼Id>

show-busy-java-threads -c <瑕佹樉绀虹殑绾跨▼鏍堟暟>

show-busy-java-threads <閲嶅鎵ц鐨勯棿闅旂鏁> [<閲嶅鎵ц鐨勬鏁>]
# 澶氭鎵ц锛涜繖2涓弬鏁扮殑浣跨敤鏂瑰紡绫讳技vmstat鍛戒护

show-busy-java-threads -a <杩愯杈撳嚭鐨勮褰曞埌鐨勬枃浠>
# 璁板綍鍒版枃浠朵互鏂逛究鍥炴函鏌ョ湅

show-busy-java-threads -S <瀛樺偍jstack杈撳嚭鏂囦欢鐨勭洰褰>
# 鎸囧畾jstack杈撳嚭鏂囦欢鐨勫瓨鍌ㄧ洰褰曪紝鏂逛究璁板綍浠ュ悗缁垎鏋

##############################
# 娉ㄦ剰锛
##############################
# 濡傛灉Java杩涚▼鐨勭敤鎴 涓 鎵ц鑴氭湰鐨勫綋鍓嶇敤鎴 涓嶅悓锛屽垯jstack涓嶄簡杩欎釜Java杩涚▼
# 涓轰簡鑳藉垏鎹㈠埌Java杩涚▼鐨勭敤鎴凤紝闇瑕佸姞sudo鏉ユ墽琛岋紝鍗冲彲浠ヨВ鍐筹細
sudo show-busy-java-threads

show-busy-java-threads -s <鎸囧畾jstack鍛戒护鐨勫叏璺緞>
# 瀵逛簬sudo鏂瑰紡鐨勮繍琛岋紝JAVA_HOME鐜鍙橀噺涓嶈兘浼犻掔粰root锛
# 鑰宺oot鐢ㄦ埛寰寰娌℃湁閰嶇疆JAVA_HOME涓斾笉鏂逛究閰嶇疆锛
# 鏄惧紡鎸囧畾jstack鍛戒护鐨勮矾寰勫氨鍙嶈屾樉寰楁洿鏂逛究浜

# -m閫夐」锛氭墽琛宩stack鍛戒护鏃跺姞涓-m閫夐」锛屾樉绀轰笂Native鐨勬爤甯э紝涓鑸簲鐢ㄦ帓鏌ヤ笉闇瑕佷娇鐢
show-busy-java-threads -m
# -F閫夐」锛氭墽琛宩stack鍛戒护鏃跺姞涓 -F 閫夐」锛堝鏋滅洿鎺stack鏃犲搷搴旀椂锛岀敤浜庡己鍒秊stack锛夛紝涓鑸儏鍐典笉闇瑕佷娇鐢
show-busy-java-threads -F
# -l閫夐」锛氭墽琛宩stack鍛戒护鏃跺姞涓 -l 閫夐」锛屾樉绀轰笂鏇村鐩稿叧閿佺殑淇℃伅锛屼竴鑸儏鍐典笉闇瑕佷娇鐢
# 娉ㄦ剰锛氬拰 -m -F 閫夐」涓璧蜂娇鐢ㄦ椂锛屽彲鑳戒細澶уぇ澧炲姞jstack鎿嶄綔鐨勮楁椂
show-busy-java-threads -l

# 甯姪淇℃伅
$ show-busy-java-threads -h
Usage: show-busy-java-threads [OPTION]... [delay [count]]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.

Example:
  show-busy-java-threads       # show busy java threads info
  show-busy-java-threads 1     # update every 1 second, (stop by eg: CTRL+C)
  show-busy-java-threads 3 10  # update every 3 seconds, update 10 times

Output control:
  -p, --pid <java pid>      find out the highest cpu consumed threads from
                            the specified java process.
                            default from all java process.
  -c, --count <num>         set the thread count to show, default is 5.
  -a, --append-file <file>  specifies the file to append output as log.
  -S, --store-dir <dir>     specifies the directory for storing
                            the intermediate files, and keep files.
                            default store intermediate files at tmp dir,
                            and auto remove after run. use this option to keep
                            files so as to review jstack/top/ps output later.
  delay                     the delay between updates in seconds.
  count                     the number of updates.
                            delay/count arguments imitates the style of
                            vmstat command.

jstack control:
  -s, --jstack-path <path>  specifies the path of jstack command.
  -F, --force               set jstack to force a thread dump. use when jstack
                            does not respond (process is hung).
  -m, --mix-native-frames   set jstack to print both java and native frames
                            (mixed mode).
  -l, --lock-info           set jstack with long listing.
                            prints additional information about locks.

CPU usage calculation control:
  -d, --top-delay           specifies the delay between top samples.
                            default is 0.5 (second). get thread cpu percentage
                            during this delay interval.
                            more info see top -d option. eg: -d 1 (1 second).
  -P, --use-ps              use ps command to find busy thread(cpu usage)
                            instead of top command.
                            default use top command, because cpu usage of
                            ps command is expressed as the percentage of
                            time spent running during the *entire lifetime*
                            of a process, this is not ideal in general.

Miscellaneous:
  -h, --help                display this help and exit.

绀轰緥

$ show-busy-java-threads
[1] Busy(57.0%) thread(23355/0x5b3b) stack of java process(23269) under user(admin):
"pool-1-thread-1" prio=10 tid=0x000000005b5c5000 nid=0x5b3b runnable [0x000000004062c000]
   java.lang.Thread.State: RUNNABLE
    at java.text.DateFormat.format(DateFormat.java:316)
    at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)
    at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:127)
    at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

[2] Busy(26.1%) thread(24018/0x5dd2) stack of java process(23269) under user(admin):
"pool-1-thread-2" prio=10 tid=0x000000005a968800 nid=0x5dd2 runnable [0x00000000420e9000]
   java.lang.Thread.State: RUNNABLE
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572)
    at java.lang.StringBuffer.append(StringBuffer.java:320)
    - locked <0x00000007908d0030> (a java.lang.StringBuffer)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:890)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869)
    at java.text.DateFormat.format(DateFormat.java:316)
    at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41)
    at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:126)
    at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

......

涓婇潰鐨勭嚎绋嬫爤鍙互鐪嬪嚭锛CPU娑堣楁渶楂樼殑2涓嚎绋嬮兘鍦ㄦ墽琛java.text.DateFormat.format锛屼笟鍔′唬鐮佸搴旂殑鏂规硶鏄shared.monitor.schedule.AppMonitorDataAvgScheduler.run銆傚彲浠ュ熀鏈‘瀹氾細

  • AppMonitorDataAvgScheduler.run璋冪敤DateFormat.format娆℃暟姣旇緝棰戠箒銆
  • DateFormat.format姣旇緝鎱€傦紙杩欎釜鍙互鐢DateFormat.format鐨勫疄鐜扮‘瀹氥傦級

澶氭墽琛屽嚑娆show-busy-java-threads锛屽鏋滀笂闈㈡儏鍐甸珮姒傜巼鍑虹幇锛屽垯鍙互纭畾涓婇潰鐨勫垽瀹氥
鍥犱负璋冪敤瓒婂皯浠g爜鎵ц瓒婂揩锛屽垯鍑虹幇鍦ㄧ嚎绋嬫爤鐨勬鐜囧氨瓒婁綆銆
鑴氭湰鏈夎嚜鍔ㄥ娆℃墽琛岀殑鍔熻兘锛屾寚瀹 閲嶅鎵ц鐨勯棿闅旂鏁/閲嶅鎵ц鐨勬鏁 鍙傛暟銆

鍒嗘瀽shared.monitor.schedule.AppMonitorDataAvgScheduler.run瀹炵幇閫昏緫鍜岃皟鐢ㄦ柟寮忥紝浠ヤ紭鍖栧疄鐜拌В鍐抽棶棰樸

璐$尞鑰

  • silentforce鏀硅繘姝よ剼鏈紝澧炲姞瀵圭幆澧冨彉閲JAVA_HOME鐨勫垽鏂 #15
  • liuyangc3
    • 鍙戠幇骞惰В鍐jstack闈炲綋鍓嶇敤鎴Java杩涚▼鐨勯棶棰樸 #50
    • 浼樺寲鎬ц兘锛岄氳繃read -a绠鍖栧弽澶嶇殑awk鎿嶄綔銆 #51
  • superhj1987 / lirenzuo
  • xiongchen2012 鎻愬嚭骞惰В鍐充簡闀跨敤鎴峰悕鎴柇鐨凚ug #62
  • qsLI / sdslnmd
    • 鍙戠幇骞舵彁浜ssue锛歴how-busy-java-threads鏀寔top鏉ヨ幏鍙朿pu鍗犵敤鐜囷紝ps鐨刢pu鍗犵敤鐜囬潪瀹炴椂 #67
  • geekMessi
    • 鍙戠幇骞舵彁浜ssue锛氬湪top v3.2涓嬫彁鍙栦笉姝g‘鐨凚ug #71
    • 鍙戠幇骞舵彁浜ssue锛歴upport command name jsvc to find java process #72

馃嵑 show-duplicate-java-classes

鉂楋笍 master鏄 涓嶅啀寮鍙戠殑鑰佺増鏈1.x鐨勫垎鏀 鉂楋笍

璇峰垏鍒 鏂扮増鏈殑寮鍙戝垎鏀


鎵惧嚭Java LibJava搴擄紝鍗Jar鏂囦欢锛夋垨Class鐩綍锛堢被鐩綍锛変腑鐨勯噸澶嶇被銆
鍏ㄧ郴缁熸敮鎸侊紙Python瀹炵幇锛屽畨瑁Python鍗冲彲锛夛紝濡LinuxMacWindows

Java寮鍙戠殑涓涓夯鐑︾殑闂鏄Jar鍐茬獊锛堝嵆澶氫釜鐗堟湰鐨Jar锛夛紝鎴栬呰閲嶅绫汇備細鍑NoSuchMethod绛夌殑闂锛岃繕涓嶈寰楀綋鏃跺嚭闂銆傛壘鍑烘湁閲嶅绫荤殑Jar锛屽彲浠ラ槻鎮f湭鐒躲

鐢ㄦ硶

  • 閫氳繃鑴氭湰鍙傛暟鎸囧畾Libs鐩綍锛屾煡鎵剧洰褰曚笅Jar鏂囦欢锛屾敹闆Jar鏂囦欢涓Class鏂囦欢浠ュ垎鏋愰噸澶嶇被銆傚彲浠ユ寚瀹氬涓Libs鐩綍銆
    娉ㄦ剰锛屽彧浼氭煡鎵捐繖涓洰褰曚笅Jar鏂囦欢锛屼笉浼氭煡鎵惧瓙鐩綍涓Jar鏂囦欢銆傚洜涓Libs鐩綍涓鑸笉浼氱敤瀛愮洰褰曞啀鏀Jar锛岃繖鏍蜂篃閬垮厤鎶婂幓鏌ユ壘涓嶆湡鏈Jar
  • 閫氳繃-c閫夐」鎸囧畾Class鐩綍锛岀洿鎺ユ敹闆嗚繖涓洰褰曚笅鐨Class鏂囦欢浠ュ垎鏋愰噸澶嶇被銆傚彲浠ユ寚瀹氬涓Class鐩綍銆
# 鏌ユ壘褰撳墠鐩綍涓嬫墍鏈塉ar涓殑閲嶅绫
show-duplicate-java-classes

# 鏌ユ壘澶氫釜鎸囧畾鐩綍涓嬫墍鏈塉ar涓殑閲嶅绫
show-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2

# 鏌ユ壘澶氫釜鎸囧畾Class鐩綍涓嬬殑閲嶅绫汇 Class鐩綍 閫氳繃 -c 閫夐」鎸囧畾
show-duplicate-java-classes -c path/to/class_dir1 -c /path/to/class_dir2

# 鏌ユ壘鎸囧畾Class鐩綍鍜屾寚瀹氱洰褰曚笅鎵鏈塉ar涓殑閲嶅绫荤殑Jar
show-duplicate-java-classes path/to/lib_dir1 /path/to/lib_dir2 -c path/to/class_dir1 -c path/to/class_dir2

# 甯姪淇℃伅
$ show-duplicate-java-classes -h
Usage: show-duplicate-java-classes [-c class-dir1 [-c class-dir2] ...] [lib-dir1|jar-file1 [lib-dir2|jar-file2] ...]

Options:
  -h, --help            show this help message and exit
  -c CLASS_DIRS, --class-dir=CLASS_DIRS
                        add class dir

JDK寮鍙戝満鏅娇鐢ㄨ鏄

Maven浣滀负鏋勫缓宸ョ▼绀烘剰杩囩▼銆

瀵逛簬涓鑸殑宸ョ▼
# 鍦ㄩ」鐩ā鍧楃洰褰曚笅鎵ц锛屾嫹璐濅緷璧朖ar鍒扮洰褰晅arget/dependency涓
$ mvn dependency:copy-dependencies -DincludeScope=runtime
...
# 妫鏌ラ噸澶嶇被
$ show-duplicate-java-classes target/dependency
...
瀵逛簬Web宸ョ▼

瀵逛簬Web宸ョ▼锛屽嵆war maven妯″潡锛屼細鎵撳寘鐢熸垚war鏂囦欢銆

# 鍦╳ar妯″潡鐩綍涓嬫墽琛岋紝鐢熸垚war鏂囦欢
$ mvn install
...
# 瑙e帇war鏂囦欢锛寃ar鏂囦欢涓寘鍚簡搴旂敤鐨勪緷璧栫殑Jar鏂囦欢
$ unzip target/*.war -d target/war
...
# 妫鏌ラ噸澶嶇被
$ show-duplicate-java-classes -c target/war/WEB-INF/classes target/war/WEB-INF/lib
...

Android寮鍙戝満鏅娇鐢ㄨ鏄

Android寮鍙戯紝鏈夐噸澶嶇被鍦ㄧ紪璇戞墦鍖呮椂浼氭姤[Dex Loader] Unable to execute dex: Multiple dex files define Lorg/foo/xxx/Yyy

浣嗗彧浼氱粰鍑轰竴涓噸澶嶇被鍚嶏紝濡傛灉閲嶅绫绘瘮杈冨鏃讹紝涓婇潰鎵撳寘/鎶ラ敊/鎺掓煡浼氳杩涜澶氭锛岃Android鐨勬墦鍖呮瘮杈冭垂鏃讹紝杩欎釜杩囩▼姣旇緝楹荤儲锛屽笇鏈涘彲浠ヤ竴娆℃妸鎵鏈夐噸澶嶇被閮藉垪鍑烘潵锛屼竴璧锋帓鏌ユ帀銆

Gradle浣滀负鏋勫缓宸ョ▼绀烘剰杩囩▼銆

Appbuild.gradle涓坊鍔犳嫹璐濆簱鍒扮洰褰build/dependencies涓嬨

task copyDependencies(type: Copy) {
    def dest = new File(buildDir, "dependencies")

    // clean dir
    dest.deleteDir()
    dest.mkdirs()

    // fill dir with dependencies
    from configurations.compile into dest
}
# 鎷疯礉渚濊禆
$ ./gradlew app:copyDependencies
...
# 妫鏌ラ噸澶嶇被
$ show-duplicate-java-classes app/build/dependencies
...

绀轰緥

$ show-duplicate-java-classes WEB-INF/lib
COOL! No duplicate classes found!

================================================================================
class paths to find:
================================================================================
1  : WEB-INF/lib/sourceforge.spring.modules.context-2.5.6.SEC02.jar
2  : WEB-INF/lib/misc.htmlparser-0.0.0.jar
3  : WEB-INF/lib/normandy.client-1.0.2.jar
...

$ show-duplicate-java-classes -c WEB-INF/classes WEB-INF/lib
Found duplicate classes in below class path:
1  (293@2): WEB-INF/lib/sourceforge.spring-2.5.6.SEC02.jar WEB-INF/lib/sourceforge.spring.modules.orm-2.5.6.SEC02.jar
2  (2@3): WEB-INF/lib/servlet-api-3.0-alpha-1.jar WEB-INF/lib/jsp-api-2.1-rev-1.jar WEB-INF/lib/jstl-api-1.2-rev-1.jar
3  (104@2): WEB-INF/lib/commons-io-2.2.jar WEB-INF/lib/jakarta.commons.io-2.0.jar
4  (6@3): WEB-INF/lib/jakarta.commons.logging-1.1.jar WEB-INF/lib/commons-logging-1.1.1.jar WEB-INF/lib/org.slf4j.jcl104-over-slf4j-1.5.6.jar
5  (344@2): WEB-INF/lib/sourceforge.spring-2.5.6.SEC02.jar WEB-INF/lib/sourceforge.spring.modules.context-2.5.6.SEC02.jar
...

================================================================================
Duplicate classes detail info:
================================================================================
1  (293@2): WEB-INF/lib/sourceforge.spring-2.5.6.SEC02.jar WEB-INF/lib/sourceforge.spring.modules.orm-2.5.6.SEC02.jar
    1   org/springframework/orm/toplink/TopLinkTemplate$13.class
    2   org/springframework/orm/hibernate3/HibernateTemplate$24.class
    3   org/springframework/orm/jpa/vendor/HibernateJpaDialect.class
    4   org/springframework/orm/hibernate3/TypeDefinitionBean.class
    5   org/springframework/orm/hibernate3/SessionHolder.class
    ...
2  (2@3): WEB-INF/lib/servlet-api-3.0-alpha-1.jar WEB-INF/lib/jsp-api-2.1-rev-1.jar WEB-INF/lib/jstl-api-1.2-rev-1.jar
    1   javax/servlet/ServletException.class
    2   javax/servlet/ServletContext.class
3  (104@2): WEB-INF/lib/commons-io-2.2.jar WEB-INF/lib/jakarta.commons.io-2.0.jar
    1   org/apache/commons/io/input/ProxyReader.class
    2   org/apache/commons/io/output/FileWriterWithEncoding.class
    3   org/apache/commons/io/output/TaggedOutputStream.class
    4   org/apache/commons/io/filefilter/NotFileFilter.class
    5   org/apache/commons/io/filefilter/TrueFileFilter.class
    ...
...

================================================================================
class paths to find:
================================================================================
1  : WEB-INF/lib/sourceforge.spring.modules.context-2.5.6.SEC02.jar
2  : WEB-INF/lib/misc.htmlparser-0.0.0.jar
3  : WEB-INF/lib/normandy.client-1.0.2.jar
4  : WEB-INF/lib/xml.xmlgraphics__batik-css-1.7.jar-1.7.jar
5  : WEB-INF/lib/jakarta.ecs-1.4.2.jar
...

璐$尞鑰

tgic鎻愪緵姝よ剼鏈傚弸鎯呰础鐚呯殑閾炬帴 commandlinefu.cn | 寰崥linux鍛戒护琛岀簿閫

馃嵑 find-in-jars

鉂楋笍 master鏄 涓嶅啀寮鍙戠殑鑰佺増鏈1.x鐨勫垎鏀 鉂楋笍

璇峰垏鍒 鏂扮増鏈殑寮鍙戝垎鏀


鍦ㄥ綋鍓嶇洰褰曚笅鎵鏈jar鏂囦欢閲岋紝鏌ユ壘绫绘垨璧勬簮鏂囦欢銆
鏀寔LinuxMacWindowscygwinMSSYS锛夈

鐢ㄦ硶

# 鍦ㄥ綋鍓嶇洰褰曚笅鎵鏈塦jar`鏂囦欢閲岋紝鏌ユ壘绫绘垨璧勬簮鏂囦欢銆
find-in-jars 'log4j\.properties'
find-in-jars 'log4j\.xml$'
find-in-jars log4j\\.xml$ # 鍜屼笂闈㈠懡浠や竴鏍凤紝Shell杞箟鐨勪笉鍚屽啓娉曡屽凡
find-in-jars 'log4j\.(properties|xml)$'

# -d閫夐」 鎸囧畾 鏌ユ壘鐩綍锛堣鐩栫己鐪佺殑褰撳墠鐩綍锛
find-in-jars 'log4j\.properties$' -d /path/to/find/directory
# 鏀寔澶氫釜鏌ユ壘鐩綍锛屽娆℃寚瀹氳繖涓夐」鍗冲彲
find-in-jars 'log4j\.properties' -d /path/to/find/directory1 -d /path/to/find/directory2

# -e閫夐」 鎸囧畾 鏌ユ壘`zip`鏂囦欢鐨勬墿灞曞悕锛岀己鐪佹槸`jar`
find-in-jars 'log4j\.properties' -e zip
# 鏀寔澶氱鏌ユ壘鎵╁睍鍚嶏紝澶氭鎸囧畾杩欎釜閫夐」鍗冲彲
find-in-jars 'log4j\.properties' -e jar -e zip

# -a閫夐」 鎸囧畾 鏌ユ壘缁撴灉涓殑Jar鏂囦欢浣跨敤缁濆璺緞
# 鍒嗕韩缁欏埆浜烘椂锛孞ar鏂囦欢璺緞鏄畬鏁寸殑锛屾柟渚垮埆浜烘壘鍒版枃浠
find-in-jars 'log4j\.properties' -a

# -s閫夐」 鎸囧畾 鏌ユ壘缁撴灉涓殑Jar鏂囦欢鍜孞ar鏂囦欢閲岀殑鏌ユ壘Entry闂村垎闅旂锛岀己鐪佹槸銆!銆
# 鏂逛究浣犲枩娆㈢殑浜虹溂鏌ョ湅锛屾垨鏄笌宸ュ叿鑴氭湰濡俙awk`鐨勫鐞
find-in-jars 'log4j\.properties' -s ' <-> '
find-in-jars 'log4j\.properties' -s ' ' | awk '{print $2}'

# 甯姪淇℃伅
$ find-in-jars -h
Usage: find-in-jars [OPTION]... PATTERN
Find files in the jar files under specified directory,
search jar files recursively(include subdirectory).
The pattern default is *extended* regex.

Example:
  find-in-jars 'log4j\.properties'
  # search file log4j.properties/log4j.xml at zip root
  find-in-jars '^log4j\.(properties|xml)$'
  find-in-jars 'log4j\.properties$' -d /path/to/find/directory
  find-in-jars '\.properties$' -d /path/to/find/dir1 -d path/to/find/dir2
  find-in-jars 'Service\.class$' -e jar -e zip
  find-in-jars 'Mon[^$/]*Service\.class$' -s ' <-> '

Find control:
  -d, --dir              the directory that find jar files.
                         default is current directory. this option can specify
                         multiply times to find in multiply directories.
  -e, --extension        set find file extension, default is jar. this option
                         can specify multiply times to find multiply extension.
  -E, --extended-regexp  PATTERN is an extended regular expression (*default*)
  -F, --fixed-strings    PATTERN is a set of newline-separated strings
  -G, --basic-regexp     PATTERN is a basic regular expression
  -P, --perl-regexp      PATTERN is a Perl regular expression
  -i, --ignore-case      ignore case distinctions

Output control:
  -a, --absolute-path    always print absolute path of jar file
  -s, --separator        specify the separator between jar file and zip entry.
                         default is `!'.

Miscellaneous:
  -h, --help             display this help and exit

娉ㄦ剰锛孭attern缂虹渷鏄grep鎵╁睍姝e垯琛ㄨ揪寮忋

绀轰緥

# 鍦ㄥ綋鍓嶇洰褰曚笅鐨勬墍鏈塉ar鏂囦欢涓紝鏌ユ壘鍑 log4j.properties鏂囦欢
$ find-in-jars 'log4j\.properties$'
./hadoop-core-0.20.2-cdh3u3.jar!log4j.properties

# 鏌ユ壘鍑 浠ervice缁撳熬鐨勭被锛孞ar鏂囦欢璺緞杈撳嚭鎴愮粷瀵硅矾寰
$ find-in-jars 'Service.class$' -a
/home/foo/deploy/app/WEB-INF/libs/spring-2.5.6.SEC03.jar!org/springframework/stereotype/Service.class
/home/foo/deploy/app/WEB-INF/libs/rpc-hello-0.0.1-SNAPSHOT.jar!com/taobao/biz/HelloService.class
......

# 鍦ㄦ寚瀹氱殑澶氫釜鐩綍鐨凧ar鏂囦欢涓紝鏌ユ壘鍑 properties鏂囦欢
$ find-in-jars '\.properties$' -d WEB-INF/lib -d ../deploy/lib | grep -v '/pom\.properties$'
WEB-INF/lib/aspectjtools-1.6.2.jar!org/aspectj/ajdt/ajc/messages.properties
WEB-INF/lib/aspectjweaver-1.8.8.jar!org/aspectj/weaver/XlintDefault.properties
../deploy/lib/groovy-all-1.1-rc-1.jar!groovy/ui/InteractiveShell.properties
../deploy/lib/httpcore-4.3.3.jar!org/apache/http/version.properties
../deploy/lib/javax.servlet-api-3.0.1.jar!javax/servlet/http/LocalStrings_es.properties
......

杩愯鏁堟灉

鏀寔褰╄壊杈撳嚭锛屾枃浠跺悕涓殑鍖归厤閮ㄥ垎浠grep鐨勯珮浜柟寮忔樉绀恒

find-in-jar screenshot

鍙傝冭祫鏂

鍦ㄥ涓狫ar(Zip)鏂囦欢鏌ユ壘Log4J閰嶇疆鏂囦欢鐨凷hell鍛戒护琛