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

I have a jar file containing as many * .class files. I need to SEARCH for one method (now I only have the method name with me), in which the class file.

Is it possible?

+6
source share
4 answers

Extract the class from the jar file and then run

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

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

+14
source

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

+2
source

You can open the jar in winzip / winrar and decompile the class file into a java file. There are several decompilers on the network

+2
source

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

  • Get entries for the specified jar file.
  • Check each JarEntry for names. If the name ends in .class. Then enter the class name.
  • Use the name of the filled class to get all methods using reflection.
  • Compare the name and name of the target method. If they are equal, type 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 the specified name of the target method.

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

And the following message is displayed in the console.

The [removeCauseMethodName] method is included in the class [org.apache.commons.lang.exception.ExceptionUtils]

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

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.

+1
source

Source: https://habr.com/ru/post/952258/


All Articles