Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 531
smilyface Avatar asked Aug 22 '13 07:08

smilyface


4 Answers

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.

like image 86
Grzegorz Żur Avatar answered Nov 08 '22 00:11

Grzegorz Żur


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.

like image 43
MouseLearnJava Avatar answered Nov 07 '22 22:11

MouseLearnJava


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

like image 2
G.S Avatar answered Nov 07 '22 23:11

G.S


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

like image 2
Crickcoder Avatar answered Nov 07 '22 23:11

Crickcoder