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

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


当前回答

如果你有一个类的实例,或者可以得到一个,你有一个纯java的解决方案:

try{
    Class clazz = Class.forName("        try{
        Class clazz = Class.forName("class-name");
        String name = "/"+clazz.getName().replace('.', '/')+".class";
        URL url = clazz.getResource(name);
        String jar = url.toString().substring(10).replaceAll("!.*", "");
        System.out.println( jar );
    }catch(Exception err){
        err.printStackTrace();
    }

当然,前两行必须根据类的实例或类名的位置进行调整。

其他回答

Unix

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

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

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

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

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

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

例如,要查找包含IdentityHashingStrategy的存档的名称:

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

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

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

语法变化:

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

窗户

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

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

下面是它的工作原理:

for /R %G in (*.jar) do - loop over all JAR files, recursively traversing directories; store the file name in %G. @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. find "ClassName" > NUL - search standard input, piped from the output of the jar command, for the given class name; this will set ERRORLEVEL to 1 iff there's a match (otherwise 0). && echo %G - iff ERRORLEVEL is non-zero, write the Java archive file name to standard output (the console).

Web

使用扫描JAR文件的搜索引擎。

要在一个jar文件夹(及其子文件夹)中找到一个类: https://jarscan.com/

Usage: java -jar jarscan.jar [-help | /?]
                    [-dir directory name]
                    [-zip]
                    [-showProgress]
                    <-files | -class | -package>
                    <search string 1> [search string 2]
                    [search string n]

Help:
  -help or /?           Displays this message.

  -dir                  The directory to start searching
                        from default is "."

  -zip                  Also search Zip files

  -showProgress         Show a running count of files read in

  -files or -class      Search for a file or Java class
                        contained in some library.
                        i.e. HttpServlet

  -package              Search for a Java package
                        contained in some library.
                        i.e. javax.servlet.http

  search string         The file or package to
                        search for.
                        i.e. see examples above

例子:

java -jar jarscan.jar -dir C:\Folder\To\Search -showProgress -class GenericServlet
#!/bin/bash

pattern=$1
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

有点晚了,但无论如何……

我一直在使用JarBrowser查找在哪个jar中存在一个特定的类。它有一个易于使用的图形用户界面,允许您浏览所选路径中所有罐子的内容。

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