查询java 类加载的路径
在Spring 3.x企业应用开发实战中看到一个能经常用到的jsp,记录下,查看类的加载路径,
在jar包冲突引起的奇葩问题时很好用。使用方法
srcAdd.jsp?className=java.net.URL
<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.security.*,java.net.*,java.io.*"%>
<%! public static URL getClassLocation(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();
// java.lang.Class contract does not specify if 'pd' can ever be null;
// it is not the case for Sun's implementations, but guard against null
// just in case:
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
// 'cs' can be null depending on the classloader behavior:
if (cs != null) result = cs.getLocation();
if (result != null) {
// Convert a code source location into a full class file location
// for some common cases:
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) {
// Try to find 'cls' definition as a resource; this is not
// document.d to be legal, but Sun's implementations seem to //allow this:
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ?
clsLoader.getResource(clsAsResource) :
ClassLoader.getSystemResource(clsAsResource);
}
return result;
}
%>
<html>
<head>
<title>srcAdd.jar</title>
</head>
<body bgcolor="#ffffff">
使用方法,className参数为类的全名,不需要.class后缀,如
srcAdd.jsp?className=java.net.URL
<%
try
{
String classLocation = null;
String error = null;
String className = request.getParameter("className"); classLocation = ""+getClassLocation(Class.forName(className));
if (error == null) {
out.print("类" + className + "实例的物理文件位于:");
out.print("<hr>");
out.print(classLocation);
}
else {
out.print("类" + className + "没有对应的物理文件。<br>");
out.print("错误:" + error);
}
}catch(Exception e)
{
out.print("异常。"+e.getMessage());
}
%>
</body>
</html>
查询java 类加载的路径的更多相关文章
- java类加载器的层次结构
类加载器的层次结构: 引导类加载器(bootstrap class loader) 用来加载java的核心库(JAVA_HOME/jre/lib/rt.jar,或sun.boot.class.path ...
- 深入理解Java类加载器(ClassLoader) (转)
转自: http://blog.csdn.net/javazejian/article/details/73413292 关联文章: 深入理解Java类型信息(Class对象)与反射机制 深入理解Ja ...
- java笔记--理解java类加载器以及ClassLoader类
类加载器概述: java类的加载是由虚拟机来完成的,虚拟机把描述类的Class文件加载到内存,并对数据进行校验,解析和初始化,最终形成能被java虚拟机直接使用的java类型,这就是虚拟机的类加载机制 ...
- java类加载器深入研究
看了下面几篇关于类的加载器的文章,豁然开朗.猛击下面的地址开始看吧. Java类加载原理解析 深入探讨 Java 类加载器 分析BootstrapClassLoader/ExtClassLo ...
- Java类加载和类反射回顾
今天学习Spring,突然想重新复习一下Java类加载和类反射的.巩固一下底层原理.部分参考了李刚老师的<疯狂Java讲义>和陈雄华.林开雄的<Spring3.x企业应用开发实战&g ...
- Java 类加载机制
类的加载: 类的初始化: 类什么时候才被初始化:1)创建类的实例,也就是new一个对象2)访问某个类或接口的静态变量,或者对该静态变量赋值3)调用类的静态方法4)反射(Class.forName(&q ...
- Java文件获取路径方式:
转自:http://blog.csdn.net/appleprince88/article/details/11599805# 谢谢! 由于经常需要获取文件的路径,但是比较容易忘记,每次需要总需要查询 ...
- 深入探讨 Java 类加载器
转自:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/ 类加载器(class loader)是 Java™中的一个很重要的概念.类 ...
- Java 类加载机制 ClassLoader Class.forName 内存管理 垃圾回收GC
[转载] :http://my.oschina.net/rouchongzi/blog/171046 Java之类加载机制 类加载是Java程序运行的第一步,研究类的加载有助于了解JVM执行过程,并指 ...
随机推荐
- UAT SIT QAS DEV PET
UAT: User Acceptance Testing 用户验收测试SIT: System Integration Testing 系统集成测试PET: Performance Evaluation ...
- Grunt_1从安装开始创建一个基本的Grunt
Grunt的介绍:http://www.gruntjs.net/getting-started 文件架构:https://github.com/zhangsai521314/Grunt 1:安装Git ...
- React-redux-webpack项目总结之用到的Es6基本语法
地址:http://blog.csdn.net/lsgqjh/article/details/53454627 http://www.cnblogs.com/hujunzheng/p/6133648. ...
- DataTable筛选出现异常
异常详细信息: System.ArgumentException: 在 Range 对象中,Min (37)必须小于或等于 max (-1). 解决方法:转化字段类型为int 再次出现问题:HH> ...
- selenium遇到readonly元素的输入
方法:去掉该元素的readonly属性 使用js来去掉 ((JavaScriptExecutor ) driver).executeScript($("input#{放置元素的CLASS}[ ...
- css权值计算
外部样式表<内部样式表<内联样式: HTML 标签选择器的权值为 1: Class 类选择器的权值为 10: ID 选择器的权值为 100: 内联样式表的权值最高 1000: !impor ...
- Web应用安全十大主动安全措施
这几天有空参加了360主办的第一届互联网安全大会(ISC 2013),除了现场看到FREEBUF现场微博号发的众多妹子之外,也听到了很多有趣的议题,比如TK分享的通过网络运营商缓存服务器偷渡,只需一台 ...
- ZACC_DOCUMENT
method if_ex_acc_document~change. data: wa_extension type bapiparex, ext_value() type c, wa_accit ty ...
- php时间设置为本地
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to ...
- c# 打乱数组
有时候得到了一个List,我想把它随机排列一下顺序.而且如果针对不同类型的List都能用,就要用到泛型. 其实思想很简单,就是从原List中每次随机取一项,添加到新的List中,并在原List中删除. ...