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 ?
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.
You can use the following steps to find all class names that include the target method name in the Jar file.
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.
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
You can open the jar in winzip/winrar and decompile the class file into java file. There are multiple decompilers available on net
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With