Java 在几十个 JAR 文件中的某个地方找到一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1342894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:27:01  来源:igfitidea点击:

Find a class somewhere inside dozens of JAR files?

javaclassjarproject-structuretext-search

提问by Kapsh

How would you find a particular class name inside lots of jar files?

如何在大量 jar 文件中找到特定的类名?

(Looking for the actual class name, not the classes that reference it.)

(寻找实际的类名,而不是引用它的类。)

采纳答案by Andreas Dolk

Eclipse can do it, just create a (temporary) project and put your libraries on the projects classpath. Then you can easily find the classes.

Eclipse 可以做到,只需创建一个(临时)项目并将您的库放在项目类路径中。然后你可以很容易地找到这些类。

Another tool, that comes to my mind, is Java Decompiler. It can open a lot of jars at once and helps to find classes as well.

我想到的另一个工具是 Java Decompiler。它可以一次打开很多罐子,也有助于查找课程。

回答by ILMTitan

I didn't know of a utility to do it when I came across this problem, so I wrote the following:

当我遇到这个问题时,我不知道有什么实用程序可以做到这一点,所以我写了以下内容:

public class Main {

    /**
     * 
     */
    private static String CLASS_FILE_TO_FIND =
            "class.to.find.Here";
    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) {
        if (!CLASS_FILE_TO_FIND.endsWith(".class")) {
            CLASS_FILE_TO_FIND = CLASS_FILE_TO_FIND.replace('.', '/') + ".class";
        }
        File start = new File(args[0]);
        if (args.length > 1) {
            CLASS_FILE_TO_FIND = args[1];
        }
        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());
                }
            } else {
                foundIn.add(f.getPath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

回答by Dave Jarvis

Unix

Unix

On Linux, other Unix variants, Git Bash on Windows, or Cygwin, use the jar(or unzip -v), grep, and findcommands.

在 Linux、其他 Unix 变体、Windows 上的 Git Bash 或 Cygwin 上,使用jar(或unzip -v) grep、 和find命令。

The following lists all class files that match a given name:

下面列出了与给定名称匹配的所有类文件:

for i in *.jar; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done

If you know the entire list of Java archives you want to search, you could place them all in the same directory using (symbolic) links.

如果您知道要搜索的 Java 档案的完整列表,则可以使用(符号)链接将它们全部放在同一目录中。

Or use find(case sensitively) to find the JAR file that contains a given class name:

或者使用find(区分大小写)查找包含给定类名的 JAR 文件:

find path/to/libs -name '*.jar' -exec grep -Hls ClassName {} \;

For example, to find the name of the archive containing IdentityHashingStrategy:

例如,要查找包含IdentityHashingStrategy以下内容的存档名称:

$ find . -name '*.jar' -exec grep -Hsli IdentityHashingStrategy {} \;
./trove-3.0.3.jar

If the JAR could be anywherein the system and the locatecommand is available:

如果 JAR 可以在系统中的任何位置并且locate命令可用:

for i in $(locate "*.jar");
  do echo "$i"; jar -tvf "$i" | grep -Hsi ClassName;
done

A syntax variation:

语法变体:

find path/to/libs -name '*.jar' -print | \
  while read i; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done 

Windows

视窗

Open a command prompt, change to the directory (or ancestor directory) containing the JAR files, then:

打开命令提示符,切换到包含 JAR 文件的目录(或祖先目录),然后:

for /R %G in (*.jar) do @jar -tvf "%G" | find "ClassName" > NUL && echo %G

Here's how it works:

这是它的工作原理:

  1. for /R %G in (*.jar) do- loop over all JAR files, recursively traversing directories; store the file name in %G.
  2. @jar -tvf "%G" |- run the Java Archive command to list all file names within the given archive, and write the results to standard output; the @symbol suppresses printing the command's invocation.
  3. find "ClassName" > NUL- search standard input, piped from the output of the jar command, for the given class name; this will set ERRORLEVELto 1 iff there's a match (otherwise 0).
  4. && echo %G- iff ERRORLEVELis non-zero, write the Java archive file name to standard output (the console).
  1. for /R %G in (*.jar) do- 循环遍历所有 JAR 文件,递归遍历目录;将文件名存储在%G.
  2. @jar -tvf "%G" |- 运行 Java Archive 命令以列出给定存档中的所有文件名,并将结果写入标准输出;该@符号禁止打印命令的调用。
  3. find "ClassName" > NUL- 搜索标准输入,从 jar 命令的输出通过管道输入给定的类名;这将设置ERRORLEVEL为 1 如果有匹配(否则为 0)。
  4. && echo %G- iffERRORLEVEL非零,将 Java 存档文件名写入标准输出(控制台)。

Web

网络

Use a search enginethat scans JAR files.

使用扫描JAR 文件搜索引擎

回答by Steve B.

You can find a class in a directory full of jars with a bit of shell:

你可以在一个装满 jars 的目录中找到一个类,其中包含一些 shell:

Looking for class "FooBar":

寻找类“FooBar”:

LIB_DIR=/some/dir/full/of/jarfiles
for jarfile in $(find $LIBDIR -name "*.jar"); do
   echo "--------$jarfile---------------"
   jar -tvf $jarfile | grep FooBar
done

回答by Wilfred Springer

Check JBoss Tattletale; although I've never used it personally, this seems to be the tool you need.

检查 JBoss Tattletale;虽然我个人从未使用过它,但这似乎是您需要的工具。

回答by Alex

One thing to add to all of the above: if you don't have the jar executable available (it comes with the JDK but not with the JRE), you can use unzip (or WinZip, or whatever) to accomplish the same thing.

要添加到上述所有内容中的一件事:如果您没有可用的 jar 可执行文件(它随 JDK 一起提供,但不随 JRE 一起提供),您可以使用 unzip(或 WinZip,或其他)来完成同样的事情。

回答by Pascal Lambert

#!/bin/bash

pattern=
shift

for jar in $(find $* -type f -name "*.jar")
do
  match=`jar -tvf $jar | grep $pattern`
  if [ ! -z "$match" ]
  then
    echo "Found in: $jar"
    echo "$match"
  fi
done

回答by user311174

grep -l "classname" *.jar

gives you the name of the jar

给你罐子的名字

find . -name "*.jar" -exec jar -t -f {} \; | grep  "classname"

gives you the package of the class

给你课程的包

回答by Liubing

Just use FindClassInJars util, it's a simple swing program, but useful. You can check source code or download jar file at http://code.google.com/p/find-class-in-jars/

只需使用 FindClassInJars util,它是一个简单的摆动程序,但很有用。您可以在http://code.google.com/p/find-class-in-jars/查看源代码或下载 jar 文件

回答by ipolevoy

some time ago, I wrote a program just for that: https://github.com/javalite/jar-explorer

前段时间,我专门为此编写了一个程序:https: //github.com/javalite/jar-explorer