Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
2015-03-27 10:24
288 查看
一、同一工程中:
测试结果:
说明:
Classloader读取文件(即方式1和方法2)都是从classpath开始查找文件; 方式3从当前类所在的目录下开始查找文件
二、不同的工程中,实验如下:
执行结果:
说明:Classloader获取的内容始终从调用方出发; 而一般类的资源获取是从类自身的位置触发
一、同一工程中:
String path = Thread.currentThread().getContextClassLoader().getResource(".").getPath(); System.out.println(path); String string = Sample1.class.getClassLoader().getResource(".").getPath(); System.out.println(string); String string2 = Sample1.class.getResource(".").getPath(); System.out.println(string2);
测试结果:
/D:/workspaces/workspace7/demo1/target/classes/ /D:/workspaces/workspace7/demo1/target/classes/ /D:/workspaces/workspace7/demo1/target/classes/com/alibaba/
说明:
Classloader读取文件(即方式1和方法2)都是从classpath开始查找文件; 方式3从当前类所在的目录下开始查找文件
二、不同的工程中,实验如下:
public class Sample2 { public static void main(String[] args) { Sample1 sample1 = new Sample1(); sample1.print(); } }
执行结果:
/D:/workspaces/workspace7/demo2/target/classes/ /D:/workspaces/workspace7/demo2/target/classes/ /D:/workspaces/workspace7/demo1/target/classes/com/alibaba/
说明:Classloader获取的内容始终从调用方出发; 而一般类的资源获取是从类自身的位置触发
相关文章推荐
- hread.currentThread().getContextClassLoader() and Class.getClassLoader()
- 区别:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- 实验:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- 区别:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- 区别:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
- Thread.currentThread().getContextClassLoader()andClass.getClassLoader()区别
- Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream
- Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream
- java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
- Thread.currentThread().getContextClassLoader()与Test.class.getClassLoader()区别
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()的区别
- Thread.currentThread().getContextClassLoader()
- Thread.currentThread().getContextClassLoader().getResourceAsStream
- Thread.currentThread().getContextClassLoader()与Test.class.getClassLoader()区别
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
- Class.getResourceAsStream(path)与Thread.currentThread().getContextClassLoader().getResourceAsStream
- 读取配置文件Properties的一种方案(Thread.currentThread().getContextClassLoader() .getResourceAsStream()