要查看jar文件中嵌入的.class文件中的方法 可能吗 ?
我有一个包含这么多* .class文件的jar文件。 我需要在其中的类文件中搜索一个方法(我现在只有我的方法名称)。
可能吗 ?
从jar文件中提取类,然后运行
unzip Classes.jar find . -name '*.class' | xargs javap -p > classes.txt
classes.txt
文件将包含有关jar内部类的所有信息。 您可以在其中搜索方法。
您可以在Eclipse IDE
看到method declaration
[但不是源代码]。 打开Package Explorer --> Referenced libraries
[在项目中引用]然后展开要查看类的jar的树。 在outline
窗口中,您可以看到方法声明
您可以在winzip / winrar中打开jar并将类文件反编译为java文件。 网上有多个反编译器
您可以使用以下步骤在Jar文件中查找包含目标方法名称的所有类名。
- 获取指定Jar文件的条目。
- 检查每个JarEntry的名称。 如果名称以’.class’结尾。 然后填充类名。
- 使用填充的类名通过reflection获取所有方法。
- 比较名称和目标方法名称。 如果它们相等,则在控制台中输出方法名称和类名称。
使用上面的步骤,我们可以在jar文件中找到具有指定目标方法名称的所有类名。
我编写了一个代码并运行了一个示例,用于在jar’comons-lang-2.4.jar ‘中找到类名,目标方法名为’ removeCauseMethodName ‘。
并在控制台中显示以下消息。
方法[removeCauseMethodName]包含在Class [org.apache.commons.lang.exception.ExceptionUtils]中
从消息中,我们可以看到类名,其中包括目标方法名。
代码如下:
注意:在运行代码之前,我们需要将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 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; } } } } }
希望这可以帮到你一些。
- java:在一个webapp中使用同一个lib的两个版本
- 尝试创建jar时清单文件中的行太长
- 尽管设置了PATH和CLASSPATH,但无法执行jar文件
- Java:为什么打包到jar文件中的代码会阻止外部类访问?
- javac:invalid flag:activation-1.1.jar
- 如何使用Eclipse创建自洽的.jar文件?
- 如何在jar中validationMANIFEST.MF的顺序?
- 如何更改JAR文件中的常规JAVA COFFEE CUP图标
- 通过Java发送电子邮件 – javax.mail.MessagingException:无法连接到SMTP主机:localhost,port:587;