首页   快速返回

getResource和getSystemResource的区别     所属分类 java
class.getResource
classLoader.getResource
ClassLoader.getSystemResource





class.getResource
不带/ 从类当前路径开始加载 , 带/ 从根路径加载


classLoader.getResource
ClassLoader.getSystemResource
不带 / 从根路径加载
带 /  返回 null 

建议使用 classLoader.getResource ,并且不要加 /


		
		ClassLoader cl = ResourceGetTest.class.getClassLoader();
		// cl = Thread.currentThread().getContextClassLoader();
        
        URL url = cl.getResource("java/lang/String.class");
        System.out.println(url);
        
        // null
        url = cl.getResource("/java/lang/String.class");
        System.out.println(url);

        
        url = ClassLoader.getSystemResource("java/lang/String.class");
        System.out.println(url);
        
        // null
        url = ClassLoader.getSystemResource("/java/lang/String.class");
        System.out.println(url);
        
        
        Class cls = ResourceGetTest.class;
        
        // null  不带 /  从类的当前路径开始加载
        url = cls.getResource("java/lang/String.class");
        System.out.println(url);
        
        // 从根路径加载
        url = cls.getResource("/java/lang/String.class");
        System.out.println(url);
        
      
jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class
null
jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class
null
null
jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class

上一篇     下一篇
linux文件中的inode

log4j日志格式配置

Class.forName()和ClassLoader.loadClass()的区别

spring常用注解

proc diskstats 字段说明

FileInputStream无法读取完整数据问题解决