概述
public class Test {
public static void getJarClassNameAndMethod(String jarFile) throws Exception {try {
File f=new File(jarFile);// 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例
URL url1=f.toURI().toURL();
URLClassLoader myClassLoader=new URLClassLoader(new URL[]{url1},Thread.currentThread().getContextClassLoader());
//通过jarFile 和JarEntry得到所有的类
JarFile jar = new JarFile(jarFile);
Enumeration<JarEntry> enumFiles = jar.entries();//返回 zip 文件条目的枚举
JarEntry entry;
while (enumFiles.hasMoreElements()) {//测试此枚举是否包含更多的元素。
entry = (JarEntry) enumFiles.nextElement();
if (entry.getName().indexOf("META-INF") < 0) {
String classFullName = entry.getName();
if (classFullName.indexOf(".class") < 0)
{
classFullName = classFullName.substring(0, classFullName.length() - 1);
}
else
{
String className = classFullName.substring(0,classFullName.length()-6).replace("/", ".");//去掉后缀 .class
Class<?> myClass = myClassLoader.loadClass(className);
//通过getMethods得到类中包含的方法
Method m[] = myClass.getMethods();
System.out.println(className); //打印类名
for(int i=0; i<m.length; i++)
{
String sm = m[i].getName();
if(DEFAULT_METHOD.indexOf(sm)<0){ //打印除默认方法外的方法
System.out.println(m[i].toString().substring((m[i].toString().indexOf(className))));
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String DEFAULT_METHOD= "waitequalsnotifynotifyAlltoStringhashCodegetClass"; //这些默认方法不用打印
public static void main(String[] args) throws Exception {
getJarClassNameAndMethod("F:\test\flowClient1.3.jar"); //Jar包所在路径
}
}
//如果出现ClassNotFoundException 说明你所反射的Jar包中 存在类依赖其他Jar包。类加载过程的一部分,你要加载的类依赖于任何类也将被加载.
There are three solutions:
-
Put the referenced Jar file somewhere that it can be found (and set the manifest property accordingly.
-
Create an Uber-jar that combines the classes in your main JAR and all of the relevant library JARs into one JAR file.
-
Write a funky class loader that knows how to load from a JAR-in-a-JAR, and modify the application to instantiate and use the classloader. (This approach is NOT recommended ...)
Note that the first two alternatives solve the problem by getting rid of your JAR-in-a-JAR structure ... in different ways.
Supposed we have a jar called Main.jar for the application. This application needs Second.jar and Third.jar . In the manifest file of the first jar (Main.jar), you adjust the Class-Path setting :
Manifest-Version: 1.0 Main-Class: MyClass Class-Path: Second.jar Third.jarThe value of the Class-Path attribute specifies the relative URLs of the extensions or libraries that this application or extension needs. URLs are separated by one or more spaces. The application or extension class loader uses the value of this attribute to construct its internal search path.
最后
以上就是幸福小蝴蝶为你收集整理的利用反射打印出 Jar包中所有的类名和方法的全部内容,希望文章能够帮你解决利用反射打印出 Jar包中所有的类名和方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复