Class.getResource和ClassLoader.getResource

个人总结

Class.getResource(path)和ClassLoader.getResource(path),

Class.getResource()从当前.class 文件路径查找资源,ClassLoader则是从jar包根目录查找.

path是指类加载器的加载范围

("") ("/") 一个是当前 .class 目录下,一个是当前.class 的根目录

Class.getResource(path)和ClassLoader.getResource(path),个人观点,两者区别不大,都是用类加载器,在取 classpath下的文件时效果一样,但要注意

ClassLoader.getResource("/")问题,返回为空,所以无法用getPath(),两者有些情况还是有区别的

@Test
public void pTest(){
String ap=Path.class.getClassLoader().getResource("").getPath();
//因为dp获得的是null,所以无法使用getPath
URL dp = Path.class.getClassLoader().getResource("/");
String bp=Path.class.getResource("").getPath();//.getPath();
String cp=Path.class.getResource("/").getPath();
System.out.println(ap);
System.out.println(dp);
System.out.println(bp);
System.out.println(cp);
}

/D:/64bit/eclipse/workspace/gzcj/WebContent/WEB-INF/classes/
null
/D:/64bit/eclipse/workspace/gzcj/WebContent/WEB-INF/classes/junit/mt/util/common/
/D:/64bit/eclipse/workspace/gzcj/WebContent/WEB-INF/classes/



在Java中获取资源的时候,经常用到Class.getResource和ClassLoader.getResource,本文给大家说一下这两者方法在获取资源文件的路径差异。
Class.getResource(String path)
path不以'/'开头时,默认是从此类所在的包下取资源;path以'/'开头时,则是从项目的ClassPath根下获取资源。在这里'/'表示ClassPath
JDK设置这样的规则,是很好理解的,path不以'/'开头时,我们就能获取与当前类所在的路径相同的资源文件,而以'/'开头时可以获取ClassPath根下任意路径的资源。
如下所示的例子:

运行结果为:
file:/D:/work_space/java/bin/net/swiftlet/
file:/D:/work_space/java/bin/

Class.getClassLoader().getResource(String path)
path不能以'/'开头时,path是指类加载器的加载范围,在资源加载的过程中,使用的逐级向上委托的形式加载的,'/'表示Boot ClassLoader中的加载范围,因为这个类加载器是C++实现的,所以加载范围为null。如下所示:

运行结果为:
file:/D:/work_space/java/bin/
null
从上面可以看出:
class.getResource("/") == class.getClassLoader().getResource("")
其实,Class.getResource和ClassLoader.getResource本质上是一样的,都是使用ClassLoader.getResource加载资源的。下面请看一下jdk的Class源码:

从上面就可以看才出来:Class.getResource和ClassLoader.getResource本质上是一样的。至于为什么Class.getResource(String path)中path可以'/'开头,是因为在name = resolveName(name);进行了处理:



InputStream   is   =   getClass().getClassLoader().getResourceAsStream("helloworld.properties");中getClass()和getClassLoader()都是什么意思呀.
getClass():取得当前对象所属的Class对象  
getClassLoader():取得该Class对象的类装载器
类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流
getClass :
public final Class getClass()
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Returns:
the object of type Class that represents the runtime class of the object.


getClassLoader
public ClassLoader getClassLoader()
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
If a security manager is present, and the caller´s class loader is not null and the caller´s class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls the security manager´s checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it´s ok to access the class loader for the class.

If this object represents a primitive type or void, null is returned.

Returns:
the class loader that loaded the class or interface represented by this object.
Throws:
SecurityException - if a security manager exists and its checkPermission method denies access to the class loader for the class.
See Also:
ClassLoader, SecurityManager.checkPermission(java.security.Permission), RuntimePermission

Class.getClassLoader()的一个小陷阱:)
昨天我的code总在Integer.class.getClassLoader().getResource("*********");这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:
jdk中关于getClassLoader()的描述:
/**
     * Returns the class loader for the class. Some implementations may use
     * null to represent the bootstrap class loader. This method will return
     * null in such implementations if this class was loaded by the bootstrap
     * class loader.
     *
     * <p> If a security manager is present, and the caller's class loader is
     * not null and the caller's class loader is not the same as or an ancestor of
     * the class loader for the class whose class loader is requested, then
     * this method calls the security manager's <code>checkPermission</code>
     * method with a <code>RuntimePermission("getClassLoader")</code>
     * permission to ensure it's ok to access the class loader for the class.
     *
     * <p>If this object
     * represents a primitive type or void, null is returned.
.....

上面的英文可以用下面的话来理解:

装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的Bootstrap ClassLoader. 为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader() 来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是Extion ClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样Extion ClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,class not found。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。

这里jdk告诉我们:如果一个类是通过bootstrap 载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader("this.getClass().getClassLoader()“),这样一来就不会有问题。


  这两个getResource()是使用当前ClassLoader加载资源(即资源在 Class path中),这样资源和class直接打在jar包中,避免文件路径问题. 
    两者不同是Class的getResource()方法是从当前.class文件路径查找资源,ClassLoader则是从jar包根目录查找. 
    注意:针对WEB的javaEE项目和javaSE项目,路径也是有所不同的。 
    下面是代码的例子,注意"/"的使用 
 


Java代码   收藏代码
  1. package com.chinaso.phl;  
  2.   
  3. /** 
  4.  * @author piaohailin 
  5.  * @date   2013-12-16 
  6. */  
  7. public class Main {  
  8.   
  9.     public static void main(String[] args) {  
  10.         System.out.println(Main.class.getResource("a.txt"));  
  11.         System.out.println(Main.class.getResource("/a.txt"));  
  12.         System.out.println(Main.class.getResource("/"));  
  13.         System.out.println(Main.class.getResource("b.txt"));  
  14.         // need to add resource/b.txt to build path! it will be package in jar file   
  15.         System.out.println(Main.class.getClassLoader().getResource("b.txt"));  
  16.         System.out.println(Main.class.getClassLoader().getResource("/"));  
  17.         System.out.println(Thread.currentThread().getContextClassLoader().getResource("b.txt"));  
  18.     }  
  19. }  


eclipse中的输出结果 
file:/I:/project/eclipse3.6/java.getResource/bin/com/chinaso/phl/a.txt 
null 
file:/I:/project/eclipse3.6/java.getResource/bin/ 
null 
file:/I:/project/eclipse3.6/java.getResource/bin/b.txt 
null 
file:/I:/project/eclipse3.6/java.getResource/bin/b.txt 


打包以后输出的结果 
C:\my>java -classpath my.jar;b.txt com.chinaso.phl.Main 
jar:file:/C:/my/my.jar!/com/chinaso/phl/a.txt 
null 
null 
null 
jar:file:/C:/my/my.jar!/b.txt 
null 
jar:file:/C:/my/my.jar!/b.txt 



用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的File类,如要取得c:/test.txt文件,就会这样用File file = newFile("c:/test.txt");这样用有什么问题,相信大家都知道,就是路径硬编码,对于JAVA精神来说,应用应该一次成型,到处可用,并且从现实应用来讲,最终生成的应用也会部署到Windows外的操作系统中,对于linux来说,在应用中用了c:/这样的字样,就是失败,所以,我们应该尽量避免使用硬编码,即直接使用绝对路径。 

  在Servlet应用中,有一个getRealPath(String str)的方法,这个方法尽管也可以动态地获得文件的路径,不秘直接手写绝对路径,但这也是一个不被建议使用的方法,那么,我们有什么方法可以更好地获得文件呢? 

     那就是Class.getResource()与Class.getResourceAsStream()方法,但很多人还是不太懂它的用法,因为很多人(比如不久前的我)都不知道应该传怎么样的参数给它,当然,有些人己经用得如火纯青,这些人是不需要照顾的,在此仅给不会或者还不是很熟的人解释一点点。 


比如我们有以下目录 
|--project 
    |--src 
        |--javaapplication 
            |--Test.java 
            |--file1.txt 
        |--file2.txt 
    |--build 
        |--javaapplication 
            |--Test.class 
            |--file3.txt 
        |--file4.txt 

在上面的目录中,有一个src目录,这是JAVA源文件的目录,有一个build目录,这是JAVA编译后文件(.class文件等)的存放目录 
那么,我们在Test类中应该如何分别获得 
file1.txt file2.txt file3.txt file4.txt这四个文件呢? 

首先讲file3.txt与file4.txt 
file3.txt: 
方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile()); 
方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile()); 
方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile()); 

file4.txt: 
方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile()); 
方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile()); 

很好,我们可以有多种方法选择,但是file1与file2文件呢?如何获得? 
答案是,你只能写上它们的绝对路径,不能像file3与file4一样用class.getResource()这种方法获得,它们的获取方法如下 
假如整个project目录放在c:/下,那么file1与file2的获取方法分别为 
file1.txt 
方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt"); 
方法二:。。。没有 

file2.txt 
方法一:File file2 = new File("c:/project/src/file2.txt"); 
方法二:。。。也没有 

总结一下,就是你想获得文件,你得从最终生成的.class文件为着手点,不要以.java文件的路径为出发点,因为真正使用的就是.class,不会拿个.java文件就使用,因为java是编译型语言嘛 

至于getResouce()方法的参数,你以class为出发点,再结合相对路径的概念,就可以准确地定位资源文件了,至于它的根目录嘛,你用不同的IDEbuild出来是不同的位置下的,不过都是以顶层package作为根目录,比如在Web应用中,有一个WEB-INF的目录,WEB-INF目录里面除了web.xml文件外,还有一个classes目录,没错了,它就是你这个WEB应用的package的顶层目录,也是所有.class的根目录“/”,假如clasaes目录下面有一个file.txt文件,它的相对路径就是"/file.txt",如果相对路径不是以"/"开头,那么它就是相对于.class的路径。。 

还有一个getResourceAsStream()方法,参数是与getResouce()方法是一样的,它相当于你用getResource()取得File文件后,再new InputStream(file)一样的结果 

   

class.getResource("/") --> 返回class文件所在的顶级目录,一般为包名的顶级目录。 --> file:/home/duanyong/workspace/cxxx/xxxx/bin/WEB-INF/classes/ 
class.getResource("/xxx.txt") --> 返回顶级目录下的xxx.txt路径。 file://..../bin/WEB-INF/classes/xxx.txt 

getResource(String path),path是以class文件的顶级目标所在的相对路径。如果顶级目录为classes,在classes/xxx/yyy.txt这样一个文件。取得yyy.txt的语法为:class.getResource("/xxx/yyy.txt"); 

示例代码: 

  1. //取得classes顶级目录下的/xxx/yyy.txt文件   
  2. System.out.println(Test.class.getResource("/xxx/yyy.txt"));   
  3. //取得本class的上路径   
  4. System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          
[Java]  view plain  copy
  1. //取得classes顶级目录下的/xxx/yyy.txt文件  
  2. System.out.println(Test.class.getResource("/xxx/yyy.txt"));  
  3. //取得本class的上路径  
  4. System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          



结果: 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/xxx/yyy.txt 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/cn/duanyong/test/Test.class

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值