利用反射打印出 Jar包中所有的类名和方法
来源:互联网 发布:湖南软件职业技术学院 编辑:程序博客网 时间:2024/05/17 00:50
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.0Main-Class: MyClassClass-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包中所有的类名和方法
- 打印出图标对应的包名和类名
- 【动态页面】(三)之一:利用Java反射机制读取Jar包的类名和属性名
- C# 利用反射,遍历获得一个类的所有属性名,方法名,成员名.
- 一道面试题:打印出类中的所有方法名
- 根据反射技术得到jar包中所有类,以及方法
- 根据反射技术得到jar包中所有类,以及方法
- JAVA 反射练习 打印指定类名的所有信息
- 【小笔记】eclipse 中隐藏项目jar包和缩短包名的方法
- Jar包内的所有类名解析
- 利用反射打印对象所有属性的工具类
- java 循环打印出某对象所在类的类名和方法
- 关于两个jar包中存在包名和类名都完全相同的jar包冲突问题
- 关于两个jar包中存在包名和类名都完全相同的jar包冲突问题
- 获取手机的所有widget类名和包名
- 打印出系统上所有的字体名
- java反射调用指定jar包中的类和方法
- 反射的具体应用反射出方法和类等等
- 夫妻相处歌谣
- java web项目 bug --canot change version of project facet dynamic web module to **
- Android学习笔记 - 废话篇
- hibernate -- list和iterator方法的区别
- dup、dup2介绍
- 利用反射打印出 Jar包中所有的类名和方法
- 数据导入与导出
- linq将字符串转化成uniqueidentifier格式失败
- Android Service学习之本地服务
- 产品经理必读:像怀胎一样怀产品,要厚着脸皮听批评
- java web框架的小段子
- google code使用方法
- UpdatePanel控件的使用(实现局部刷新,ajax)
- 翅与力量: