Java 如何找到包含类定义的 jar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3497664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find the jar file containing a class definition?
提问by luiscolorado
Is there a tool, plugin, or script I can use to find a java class in a bunch of jar files?
是否有工具、插件或脚本可用于在一堆 jar 文件中查找 java 类?
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.
我经常继承抱怨类不存在的代码,这只是因为 jar 文件未包含在类路径中。但是,类在哪个 jar 文件中?我可能没有 JAR(所以我必须在线搜索),或者将 JAR 添加到类路径可能会产生重复的类定义问题。
I obviously would prefer an eclipse plugin, but I'm open to any piece of software that works with Windows.
我显然更喜欢 Eclipse 插件,但我对任何适用于 Windows 的软件都持开放态度。
I know... Windows is not my choice, but that's what I got to work with.
我知道... Windows 不是我的选择,但这就是我必须使用的。
Thanks!
谢谢!
Luis
路易斯
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.
PS谢谢你的回答。在查看了一些回复后,我意识到我应该更好地解释我的场景。我们有一个下载或创建的 JAR 文件库,但有时课程会在某个地方在线。
回答by Nikita Rybak
I usually employ bash for that: grep -lr "ClassName" .
The trick is that names aren't encoded in any way. You can open jar file in text editor and you'll see them. (You can even include package name in search query.)
我通常为此使用 bash:grep -lr "ClassName" .
诀窍是名称没有以任何方式编码。您可以在文本编辑器中打开 jar 文件,您会看到它们。(您甚至可以在搜索查询中包含包名称。)
I suspect, there's some windows equivalent too.
我怀疑,也有一些等效的窗口。
回答by Arunabh Das
You could always add the Reference library to your project in Eclipse and then in Package Browser, just expand the packages in the JAR file until you find the class that you are looking for.
您始终可以在 Eclipse 中将 Reference 库添加到您的项目中,然后在包浏览器中,只需展开 JAR 文件中的包,直到找到您要查找的类。
回答by NinjaCat
@Nikita Rybak: AstroGrep for Windows is our friend: http://astrogrep.sourceforge.net/
@Nikita Rybak:Windows 版 AstroGrep 是我们的朋友:http://astrogrep.sourceforge.net/
回答by BalusC
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.
我经常继承抱怨类不存在的代码,这只是因为 jar 文件未包含在类路径中。
If it's not in the classpath, then you likely don't have the JAR file itself at all. Searching via Eclipse's builtin Ctrl+Shift+T
function won't help much. Usually you can make use of the package name to "guess" where you could get the JAR file from at the internet. E.g. a org.apache.commons.lang.XXX
class is available at http://commons.apache.org/lang.
如果它不在类路径中,那么您可能根本没有 JAR 文件本身。通过 Eclipse 的内置Ctrl+Shift+T
函数进行搜索不会有太大帮助。通常,您可以使用包名称来“猜测”您可以从 Internet 获取 JAR 文件的位置。例如,org.apache.commons.lang.XXX
在http://commons.apache.org/lang上提供了一个类。
For the unobvious ones, I myself use http://grepcode.com, the JAR source code search engine.
对于不明显的,我自己使用http://grepcode.com,JAR 源代码搜索引擎。
回答by ILMTitan
use this:
用这个:
public class Main {
private static final String SEARCH_PATH = "C:\workspace\RPLaunch";
private static String CLASS_FILE_TO_FIND =
"javax.ejb.SessionBean";
private static List<String> foundIn = new LinkedList<String>();
/**
* @param args the first argument is the path of the file to search in. The second may be the
* class file to find.
*/
public static void main(String[] args) {
File start;
new Scanner(args[0]);
if (args.length > 0) {
start = new File(args[0]);
if (args.length > 1) {
CLASS_FILE_TO_FIND = args[1];
}
} else {
start = new File(SEARCH_PATH);
}
if (!CLASS_FILE_TO_FIND.endsWith(".class")) {
CLASS_FILE_TO_FIND = CLASS_FILE_TO_FIND.replace('.', '/') + ".class";
}
search(start);
System.out.println("------RESULTS------");
for (String s : foundIn) {
System.out.println(s);
}
}
private static void search(File start) {
try {
final FileFilter filter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jar") || pathname.isDirectory();
}
};
for (File f : start.listFiles(filter)) {
if (f.isDirectory()) {
search(f);
} else {
searchJar(f);
}
}
} catch (Exception e) {
System.err.println("Error at: " + start.getPath() + " " + e.getMessage());
}
}
private static void searchJar(File f) {
try {
System.out.println("Searching: " + f.getPath());
JarFile jar = new JarFile(f);
ZipEntry e = jar.getEntry(CLASS_FILE_TO_FIND);
if (e == null) {
e = jar.getJarEntry(CLASS_FILE_TO_FIND);
}
if (e != null) {
foundIn.add(f.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by Fanny H.
In the same lines as BalusC's answer (I can't post comment yet nor link 2 urls, no reputation :( ), you can find a jar thanks to these 2 jar finder engines: - http://www.jarfinder.com/- findjar
与 BalusC 的回答相同(我还不能发表评论,也不能链接 2 个网址,没有声誉:(),由于这两个 jar 查找引擎,您可以找到一个 jar:- http://www.jarfinder.com/- 查找jar
回答by ipolevoy
I have written a program for this: https://github.com/javalite/jar-explorerIt will also decompile existing byte code to show you interfaces, methods, super classes, will show contents of other resources - text, images, html, etc.
我为此编写了一个程序:https: //github.com/javalite/jar-explorer它还将反编译现有的字节码以向您显示接口、方法、超类,将显示其他资源的内容 - 文本、图像、html , 等等。
回答by Rob
I ran into the same problem countless times, so I wrote a little tool to find them.
我无数次遇到同样的问题,所以我写了一个小工具来找到它们。
A simple command line: findclass MyClass -classpath ...
一个简单的命令行: findclass MyClass -classpath ...
It searches directories, jar, zip, war and ear files for your class. I wrote it a few years back and use it all the time. I thought others might find it useful too so I uploaded onto github.
它为您的班级搜索目录、jar、zip、war 和 ear 文件。几年前我写了它并且一直在使用它。我认为其他人可能也会觉得它很有用,所以我上传到了 github。
It's freely available to download: https://github.com/eurozulu/Findclass/tree/master/dist
可免费下载:https: //github.com/eurozulu/Findclass/tree/master/dist
If you want to look at the source, it's in the same site: https://github.com/eurozulu/Findclass/tree/master
如果你想看源码,它在同一个站点:https: //github.com/eurozulu/Findclass/tree/master
If you like it, just let me know to give me that warm comfy feeling :)
如果你喜欢它,请告诉我给我那种温暖舒适的感觉:)
回答by Mike Pone
回答by Marcus Junius Brutus
(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.)
(这是对我在以前版本的答案中的脚本的改进,因为它以一些awk
特殊/丑陋的引用为代价产生了更清晰的输出。)
I've built a script (findinjars
) which does just that.
我已经构建了一个脚本 ( findinjars
) 来做到这一点。
#!/usr/bin/env bash
if [[ ($# -ne 1) && ($# -ne 2) ]]
then
echo "usage is $findinjars a.b.c.d.Class [directoryTreeRoot or, if missing, current dir]
<grep pattern to look for in 'jar tvf' output> [<top-of-dir-tree> or, if missing, current dir]"
else
THING_TO_LOOKFOR=""
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"'" " " $findinjars partOfClassName [directoryTreeRoot or, if missing, current dir]
}' | grep -i "$THING_TO_LOOKFOR") ; done
fi
you can then invoke it with:
然后你可以调用它:
##代码##or just
要不就
##代码##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.
完全限定类名中的点字符将在“任何字符”的正则表达式意义上进行解释,但这没什么大不了的,因为这是一个启发式实用程序(这就是为什么它也不区分大小写)。通常我不关心完整的类名,只输入其中的一部分。