import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class jarTest { public static void getJarName(String jarFile) throws Exception { try{ //通過將給定路徑名字符串轉換為抽象路徑名來創建一個新File實例 File f = new File(jarFile); URL url1 = f.toURI().toURL(); URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1},Thread.currentThread().getContextClassLoader()); //通過jarFile和JarEntry得到所有的類 JarFile jar = new JarFile(jarFile); //返回zip文件條目的枚舉 Enumeration<JarEntry> enumFiles = jar.entries(); JarEntry entry; //測試此枚舉是否包含更多的元素 while(enumFiles.hasMoreElements()){ entry = (JarEntry)enumFiles.nextElement(); if(entry.getName().indexOf("META-INF")<0){ String classFullName = entry.getName(); if(!classFullName.endsWith(".class")){ classFullName = classFullName.substring(0,classFullName.length()-1); } else{ //去掉后綴.class String className = classFullName.substring(0,classFullName.length()-6).replace("/", "."); Class<?> myclass = myClassLoader.loadClass(className); //打印類名 System.out.println("*****************************"); System.out.println("全類名:" + className); //得到類中包含的屬性 Method[] methods = myclass.getMethods(); for (Method method : methods) { String methodName = method.getName(); System.out.println("方法名稱:" + methodName); Class<?>[] parameterTypes = method.getParameterTypes(); for (Class<?> clas : parameterTypes) { // String parameterName = clas.getName(); String parameterName = clas.getSimpleName(); System.out.println("參數類型:" + parameterName); } System.out.println("=========================="); } } } } } catch(IOException e){ e.printStackTrace(); } } /** * 這些默認方法不打印 */ private static String DEFAULT_METHOD = "waitequalsnotifynotifyAlltoStringhashCodegetClass"; public static void main(String[] args) throws Exception { //jar包所在路徑 /*getJarName("F:\\user.jar"); getJarName("F:\\role1.jar"); getJarName("F:\\role2.jar"); */ //getJarName("F:\\UserInfo.jar"); getJarName("F:\\test\\FileTest.jar"); } }