Java 查看嵌入在 jar 文件中的 .class 文件中的方法 | 是否可以 ?

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

To see method in .class file embedded in jar file | is it possible ?

javaclassmethodsjar

提问by smilyface

I have a jar file containing so many *.class files. I need to SEARCH one method (i just have the method name alone with me now) in which class file.

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

Is it possible ?

是否可以 ?

采纳答案by Grzegorz ?ur

Extract the class from jar file and then run

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

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

The classes.txtfile will have all information about the classes inside jar. You can search it for a method.

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

回答by G.S

You can see the method declaration[But not the source code] in Eclipse IDE. Open Package Explorer --> Referenced libraries[which are referenced in your project] then expand the tree for the jar which you want to see the classes. In the outlinewindow, you can see the method declaration

您可以method declarationEclipse IDE. 打开Package Explorer --> Referenced libraries[在您的项目中引用],然后展开要查看类的 jar 的树。在outline窗口中,可以看到方法声明

回答by Gaurav Varma

You can open the jar in winzip/winrar and decompile the class file into java file. There are multiple decompilers available on net

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

回答by MouseLearnJava

You can use the following steps to find all class names that include the target method name in the Jar file.

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

  1. Get the Entries for a specified Jar File.
  2. Check each JarEntry for the names. If the name ends with '.class'. Then Populate the class name.
  3. Use the populated class name to get all methods through reflection.
  4. Compare the name and target method name. If they are equal, then print the method name and class name in the console.
  1. 获取指定 Jar 文件的条目。
  2. 检查每个 JarEntry 的名称。如果名称以“.class”结尾。然后填充类名。
  3. 使用填充的类名通过反射获取所有方法。
  4. 比较名称和目标方法名称。如果它们相等,则在控制台中打印方法名称和类名称。

Use the above steps, we can find all the class names in the jar file with a specified target method name.

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

I wrote a code and ran an example for finding class name in jar 'commons-lang-2.4.jar' with target method name 'removeCauseMethodName'.

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

And the following message is displaying in console.

以下消息显示在控制台中。

Method [removeCauseMethodName] is included in Class [org.apache.commons.lang.exception.ExceptionUtils]

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

From the message, we can see the class name, which includes the target method name.

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

The code is as follows:

代码如下:

Note:before running the code, we need to add jar 'commons-lang-2.4.jar' to the build path.

注意:在运行代码之前,我们需要将 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;
                }
            }

        }
    }
}

Hope this can help you some.

希望这可以帮助你一些。