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

后端 未结 4 2233
不知归路
不知归路 2021-02-14 05:04

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.

Is it possible ?

相关标签:
4条回答
  • 2021-02-14 05:18

    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 outline window, you can see the method declaration

    0 讨论(0)
  • 2021-02-14 05:18

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

    0 讨论(0)
  • 2021-02-14 05:23

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

    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.

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

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

    And the following message is displaying in console.

    Method [removeCauseMethodName] is included in Class [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.

    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.

    0 讨论(0)
  • 2021-02-14 05:45

    Extract the class from jar file and then run

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

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

    0 讨论(0)
提交回复
热议问题