• 读取指定jar包 解析jar包中所有的类(获取到类的方法和属性)


    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");  
    	    	
    	    }  
    	
    }
    
  • 相关阅读:
    watch监听不到data数据
    滑块验证码 纯js
    position: fixed居中
    Vue插件之缺口滑块验证码 适合于手机和pc(插件vuemonoplastyslideverify)
    Vue之滑动验证码 适合于手机和pc JcRange(可分辨系统)
    Bus
    Table表中添加下拉框
    容器docker网络解析
    jstack定位java程序CPU使用高问题
    <摘>C#,MFC,Win32——实现系统级热键
  • 原文地址:https://www.cnblogs.com/huangzhimin/p/6496562.html
Copyright © 2020-2023  润新知