【问题标题】:To see method in .class file embedded in jar file | is it possible ?查看嵌入在 jar 文件中的 .class 文件中的方法 |可能吗 ?
【发布时间】:2013-08-24 19:14:05
【问题描述】:

我有一个包含这么多 *.class 文件的 jar 文件。我需要在哪个类文件中搜索一种方法(我现在只有方法名称)。

有可能吗?

【问题讨论】:

  • 您是想查看方法的来源还是仅按名称查找?
  • hexafraction ,只需要按名称查找即可。

标签: java class methods jar


【解决方案1】:

从jar文件中提取类然后运行

unzip Classes.jar
find . -name '*.class' | xargs javap -p > classes.txt

classes.txt 文件将包含有关 jar 中的类的所有信息。您可以搜索它的方法。

【讨论】:

  • 您能否编写更多脚本,以便 OP 可以搜索或 grep?
  • @hexafraction 添加了一个脚本以从 jar 中获取所有类。
  • @zip,是的,我愿意,如果你愿意,你可以为 Windows 添加答案。
【解决方案2】:

您可以使用以下步骤在Jar文件中查找所有包含目标方法名称的类名称。

  1. 获取指定 Jar 文件的条目。
  2. 检查每个 JarEntry 的名称。如果名称以“.class”结尾。然后填充类名。
  3. 使用填充的类名通过反射获取所有方法。
  4. 比较名称和目标方法名称。如果相等,则在控制台中打印方法名和类名。

通过以上步骤,我们可以在jar文件中找到指定目标方法名的所有类名。

我编写了一个代码并运行了一个示例,用于在目标方法名称为“removeCauseMethodName”的 jar 'commons-lang-2.4.jar' 中查找类名。。 p>

控制台中显示以下消息。

方法 [removeCauseMethodName] 包含在类 [org.apache.commons.lang.exception.ExceptionUtils] 中

从消息中,我们可以看到类名,其中包括目标方法名。

代码如下:

注意:在运行代码之前,我们需要将jar 'commons-lang-2.4.jar' 添加到构建路径中。

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class SearchMetodInJarFile {

    private static final String CLASS_SUFFIX = ".class";

    public static void main(String[] args) throws IOException,
            SecurityException, ClassNotFoundException {

        /** target method name to be searched */
        String targetMethodClass = "removeCauseMethodName";

        /**
         * Specify a target method name as 'removeCauseMethodName'. Find class
         * name that includes the target method name in Jar File.
         */
        new SearchMetodInJarFile().searchMethodName(new JarFile(
                "D:\\Develop\\workspace\\Test\\commons-lang-2.4.jar"),
                targetMethodClass);

    }

    /**
     * Search target method name in multiple Jar files.
     */
    public void searchMethodName(JarFile[] jarFiles, String targetMethodName)
            throws SecurityException, ClassNotFoundException {

        for (JarFile jarFile : jarFiles) {
            searchMethodName(jarFile, targetMethodName);
        }
    }

    /**
     * Search target method name in one Jar file.
     */
    public void searchMethodName(JarFile jarFile, String targetMethodName)
            throws SecurityException, ClassNotFoundException {
        Enumeration<JarEntry> entryEnum = jarFile.entries();
        while (entryEnum.hasMoreElements()) {
            doSearchMethodName(entryEnum.nextElement(), targetMethodName);
        }
    }

    /**
     * Check the name of JarEntry, if its name ends with '.class'. Then do the
     * following 3 steps: 1. Populate Class name. 2. Get the methods by
     * reflection. 3. Compare the target method name with the names. If the
     * methood name is equal to target method name. Then print the method name
     * and class name in console.
     */
    private void doSearchMethodName(JarEntry entry, String targetMethodName)
            throws SecurityException, ClassNotFoundException {
        String name = entry.getName();
        if (name.endsWith(CLASS_SUFFIX)) {
            /**
             * Populate the class name
             */
            name = name.replaceAll("/", ".")
                    .substring(0, name.lastIndexOf("."));

            /**
             * Retrieve the methods via reflection.
             */
            Method[] methods = Class.forName(name).getDeclaredMethods();
            for (Method m : methods) {
                /**
                 * Print the message in console if the method name is expected.
                 */
                if (targetMethodName.equals(m.getName())) {
                    System.out.println(String.format(
                            "Method [%s] is included in Class [%s]",
                            targetMethodName, name));
                    break;
                }
            }

        }
    }
}

希望对你有所帮助。

【讨论】:

  • 非常感谢您制作这个。我已经接受了 linux 命令的答案。但我仍然喜欢你的工作。
  • 我在使用这个方法时遇到了一个问题,即使.class确实存在于jar文件中,它也会抛出ClassNotFoundException的异常。
【解决方案3】:

你可以在winzip/winrar中打开jar,将class文件反编译成java文件。 网上有多种反编译器可供使用

【讨论】:

  • 不,OP要搜索,
【解决方案4】:

您可以在Eclipse IDE 中看到method declaration [但不是源代码]。 打开Package Explorer --&gt; Referenced libraries [在您的项目中引用],然后展开您想要查看类的 jar 的树。 在outline窗口中可以看到方法声明

【讨论】:

  • 简明扼要。它可以在 Windows 或 Unix/Linux 中使用,因为它位于 Eclipse IDE 中。
  • 关于@G.S 解决方案的信息性屏幕截图。 (stackoverflow 不允许我回复帖子,因为我的声誉低于 50。酷。)![enter image description here](i.stack.imgur.com/ls2Ui.jpg)
猜你喜欢
  • 2022-01-17
  • 2010-09-16
  • 2013-01-02
  • 2013-06-07
  • 2018-04-01
  • 1970-01-01
  • 2012-12-13
  • 2010-12-12
  • 2013-09-17
相关资源
最近更新 更多