java url jar包_Java绝对路径读取jar包 | 学步园

方法一:

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> classes = new LinkedHashSet>();//所有的Class对象

Map, Annotation[]> classAnnotationMap = new HashMap, Annotation[]>();//每个Class对象上的注释对象

Map, Map> classMethodAnnoMap = new HashMap, Map >();//每个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 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 methodAnnoMap = new HashMap();

for(int i = 0;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();

}

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值