如何找到包含类定义的 jar 文件? [英] how to find the jar file containing a class definition?
问题描述
是否有工具、插件或脚本可以用来在一堆 jar 文件中查找 java 类?
Is there a tool, plugin, or script I can use to find a java class in a bunch of jar files?
很多时候我继承的代码抱怨一个不存在的类,这只是因为类路径中没有包含 jar 文件.但是,类在哪个 jar 文件中?我可能没有 JAR(所以我必须在线搜索),或者将 JAR 添加到类路径可能会产生重复的类定义问题.
Very often I inherit code that complains about a class that doesn't exist, and it is just because the jar file is not included in the classpath. But, in what jar file(s) is the class? I may not have the JAR (so I have to search online), or adding a JAR to the classpath could create a duplicated class definition problem.
我显然更喜欢 Eclipse 插件,但我对任何适用于 Windows 的软件都持开放态度.
I obviously would prefer an eclipse plugin, but I'm open to any piece of software that works with Windows.
我知道... Windows 不是我的选择,但这就是我必须使用的.
I know... Windows is not my choice, but that's what I got to work with.
谢谢!
路易斯
附言谢谢您的回答.在查看了一些回复后,我意识到我应该更好地解释我的场景.我们有一个下载或创建的 JAR 文件库,但有时该类会在某个地方在线.
P.S. Thank you for your answers. After reviewing some responses, I became aware that I should have explained better my scenario. We had a library of downloaded or created JAR files, but sometimes the class would be online somewhere.
推荐答案
(这是对我在以前版本的答案中的脚本的改进,因为它以一些 的代价产生了更清晰的输出awk
特殊/丑陋的引用.)
(This is an improvement over the script I had in previous versions of the answer as it produces much cleaner output at the price of some awk
special/ugly quoting.)
我已经构建了一个脚本 (findinjars
) 来做到这一点.
I've built a script (findinjars
) which does just that.
#!/usr/bin/env bash
if [[ ($# -ne 1) && ($# -ne 2) ]]
then
echo "usage is $0 <grep pattern to look for in 'jar tvf' output> [<top-of-dir-tree> or, if missing, current dir]"
else
THING_TO_LOOKFOR="$1"
DIR=${2:-.}
if [ ! -d $DIR ]; then
echo "directory [$DIR] does not exist";
exit 1;
fi
find "$DIR" -iname *.jar | while read f ; do (jar tf $f | awk '{print "'"$f"'" " " $0}' | grep -i "$THING_TO_LOOKFOR") ; done
fi
然后你可以调用它:
$findinjars a.b.c.d.Class [directoryTreeRoot or, if missing, current dir]
或者只是
$findinjars partOfClassName [directoryTreeRoot or, if missing, current dir]
完全限定类名中的点字符将在任何字符"的正则表达式意义上进行解释,但这没什么大不了的,因为这是一个启发式实用程序(这就是为什么它也不区分大小写).通常我不关心完整的类名,只输入它的一部分.
Dot characters in the fully qualified class name will be interpreted in the regexp sense of 'any character' but that's not a big deal since this is a heuristics utility (that's why it's case-insensitive too BTW). Usually I don't bother with the full class name and just type part of it.
这篇关于如何找到包含类定义的 jar 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!