现在的位置: 首页 > 综合 > 正文

Java绝对路径读取jar包

2018年05月09日 ⁄ 综合 ⁄ 共 2676字 ⁄ 字号 评论关闭

方法一:

package com.demo.action;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			URL url = new URL("file:/D:/maven_web.jar");
			@SuppressWarnings("resource")
			URLClassLoader myClassLoader  =   new  URLClassLoader( new  URL[]   { url } );
			Class<?> myClass  =  myClassLoader.loadClass("com.huahua.inter.impl.UserImpl");
			Method[] methods = myClass.getDeclaredMethods();
			for (Method method : methods) {
				System.out.println(method.getName());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}

方法二:

package com.demo.action;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class test1 {
	 public static void main(String[] args) {
		 String path = "D:\\maven_web.jar";//外部jar包的路径  
		 Set<Class<?>> classes = new LinkedHashSet<Class<?>>();//所有的Class对象  
		 Map<Class<?>, Annotation[]> classAnnotationMap = new HashMap<Class<?>, Annotation[]>();//每个Class对象上的注释对象  
		 Map<Class<?>, Map<Method, Annotation[]>> classMethodAnnoMap = new HashMap<Class<?>, Map<Method,Annotation[]> >();//每个Class对象中每个方法上的注释对象  
		 
		 try {
			 @SuppressWarnings("resource")
			 JarFile jarFile = new JarFile(new File(path));  
			 URL url = new URL("file:" + path);  
			 @SuppressWarnings("resource")
			 ClassLoader loader = new URLClassLoader(new URL[]{url});//自己定义的classLoader类,把外部路径也加到load路径里,使系统去该路经load对象  
			 Enumeration<JarEntry> es = jarFile.entries();  
			 while (es.hasMoreElements()) { 
				 JarEntry jarEntry = (JarEntry) es.nextElement();  
				 String name = jarEntry.getName(); 
				 //System.out.println(name);
				 if(name != null && name.endsWith(".class")){//只解析了.class文件,没有解析里面的jar包  
					 //默认去系统已经定义的路径查找对象,针对外部jar包不能用  
					 //Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(name.replace("/", ".").substring(0,name.length() - 6));  
					 Class<?> c = loader.loadClass(name.replace("/", ".").substring(0,name.length() - 6));//自己定义的loader路径可以找到
					 
					 if(c.isInterface()) continue; //排除接口类
						 
					 System.out.println(c);  
					 classes.add(c);  
					 Annotation[] classAnnos = c.getDeclaredAnnotations();  
					 classAnnotationMap.put(c, classAnnos);  
					 Method[] classMethods = c.getDeclaredMethods();  
					 for (Method method : classMethods) {
						System.out.println(method.getName());//打印所有自定义的方法
					 }
					 Map<Method, Annotation[]> methodAnnoMap = new HashMap<Method, Annotation[]>();  
					 for(int i = 0;i<classMethods.length;i++){  
						 Annotation[] a = classMethods[i].getDeclaredAnnotations();  
						 methodAnnoMap.put(classMethods[i], a);  
					 }  
					 classMethodAnnoMap.put(c, methodAnnoMap);  
				 }  
			 }  
			 System.out.println(classes.size());  
		 } catch (Exception e) {  
			 e.printStackTrace();  
		 } 
	}
}

方法二转自:http://blog.csdn.net/wan368500/article/details/8215668

抱歉!评论已关闭.