通过类反射找到对应的jar位置
来源:互联网 发布:网络丢包怎么解决 编辑:程序博客网 时间:2024/06/01 16:41
在使用构建工具maven开发时,需要找到某个类在那个jar,但是每次一点开就直接源码了,有时候load不到倒是显示了路径,所以这对我造成了一些困扰。
/** * 获取类所在的jar包位置 * */public class ClassLocationUtils { /** * 获取类所有的路径 * * @param cls * @return */ public static String where(final Class cls) { if (cls == null) throw new IllegalArgumentException("null input: cls"); URL result = null; final String clsAsResource = cls.getName().replace('.', '/') .concat(".class"); final ProtectionDomain pd = cls.getProtectionDomain(); if (pd != null) { final CodeSource cs = pd.getCodeSource(); if (cs != null) result = cs.getLocation(); if (result != null) { if ("file".equals(result.getProtocol())) { try { if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip")) result = new URL("jar:" .concat(result.toExternalForm()) .concat("!/").concat(clsAsResource)); else if (new File(result.getFile()).isDirectory()) result = new URL(result, clsAsResource); } catch (MalformedURLException ignore) { } } } } if (result == null) { final ClassLoader clsLoader = cls.getClassLoader(); result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource); } return result.toString(); }}
测试
@Test public void show(){ String location=ClassLocationUtils.where(AnnotationVisitor.class); System.out.println(location); }
结果
jar:file:/I:/MAVEN_LIb/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar!/org/springframework/asm/AnnotationVisitor.class
1 0
- 通过类反射找到对应的jar位置
- 如何通过类名找到对应的dex文件
- 通过词找到对应的文章类别
- 通过USB口找到对应的event
- eclipse通过类名,找到所在的jar包
- JAVA语言实现大量信息的分页显示功能及对象反射(通过参数找到对应的函数)
- 通过反射找到私有域的值
- SparkSQL:如何找到与Spark版本对应的jar包
- 找到jar包冲突位置
- 在myeclipse中如何通过jsp页面找到对应的java类文件
- 通过地址url在myeclipse中找到对应的程序
- 通过一个数组找到每个元素对应出现的次数
- OD通过条件断点快速找到弹出对话框的位置
- 通过位置找到ListView中的某个item的View
- 如何通过一张图片找到你的位置?
- 在运行时通过反射找到被注释的方法
- MyEclipse中如何通过一个类名找到它所属的jar包
- Java工具类之通过IP获取对应的位置信息及网络供应商
- django mysql安装及相关说明
- Buffer中的flip
- 显卡结构及工作原理详细解读
- Faster R-CNN训练与测试
- 使用基本 SQL 命令
- 通过类反射找到对应的jar位置
- php推荐使用的文件名声明与SQL的基本语句汇总
- 前端朋友跳槽必读(前端知识大杂烩)
- java设计模式
- spring拦截器配置
- zabbix自带mysql模板监控mysql
- 第十章 利用k-均值聚类算法对未标注数据分组
- oracle全托管驱动Oracle.ManagedDataAccess
- 遇到的问题----新建maven项目ArtifactTransferException 和Execution default-testResources of goal或者导入maven报错