Class.getResource and ClassLoader.getSystemResource: is there a reason to prefer one to another?

后端 未结 4 2096
借酒劲吻你
借酒劲吻你 2020-12-04 16:40

I saw both Class.getResource and ClassLoader.getSystemResource used to locate a resource in Java. Is there any reason to prefer one to another?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 17:23

    Using Methods of java.lang.Class:

    public java.net.URL getResource(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader();
      if (cl==null) {
        return ClassLoader.getSystemResource(name);  // A system class.
      }
      return cl.getResource(name);
    }
    

提交回复
热议问题