What is the difference between Class.getResource() and ClassLoader.getResource()?

前端 未结 7 1874
情歌与酒
情歌与酒 2020-11-22 02:25

I wonder what the difference is between Class.getResource() and ClassLoader.getResource()?

edit: I especially want to know if any cachi

7条回答
  •  -上瘾入骨i
    2020-11-22 03:05

    All these answers around here, as well as the answers in this question, suggest that loading absolute URLs, like "/foo/bar.properties" treated the same by class.getResourceAsStream(String) and class.getClassLoader().getResourceAsStream(String). This is NOT the case, at least not in my Tomcat configuration/version (currently 7.0.40).

    MyClass.class.getResourceAsStream("/foo/bar.properties"); // works!  
    MyClass.class.getClassLoader().getResourceAsStream("/foo/bar.properties"); // does NOT work!
    

    Sorry, I have absolutely no satisfying explanation, but I guess that tomcat does dirty tricks and his black magic with the classloaders and cause the difference. I always used class.getResourceAsStream(String) in the past and haven't had any problems.

    PS: I also posted this over here

提交回复
热议问题