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

后端 未结 4 2098
借酒劲吻你
借酒劲吻你 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:28

    From Class.getResource( )

    This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.

    and ClassLoader.getSystemResource( )

    Find a resource of the specified name from the search path used to load classes. This method locates the resource through the system class loader

提交回复
热议问题