Class.getResource vs. ClassLoader.getResource
Language/JAVA 2014. 11. 14. 11:23Java API Documentation에서 보면
public URL getResource(String name)
- Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to
ClassLoader.getSystemResource(java.lang.String)
.Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
- Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
public URL getResource(String name)
- Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a '/'-separated path name that identifies the resource.
This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke
findResource(String)
to find the resource.
Class.getResource() 메소드는 해당 클래스의 소스 파일 위치를 상대 루트로 설정한다.
반면, ClassLoader.getResource()/getResources() 메소드는 클래스패스의 루트를 상대 루트로 설정한다.
다음은 이와 같은 차이를 보여주는 예제이다.
package test;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
public class GetResourceExample {
public static void main(String[] args) {
Class<GetResourceExample> clazz = GetResourceExample.class;
URL resource = clazz.getResource(".");
System.out.println("resource: " + resource);
ClassLoader classLoader = clazz.getClassLoader();
try {
Enumeration<URL> resources = classLoader.getResources(".");
System.out.println("resources:");
while (resources.hasMoreElements()) {
URL nextElement = resources.nextElement();
System.out.println(nextElement);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
결과는 다음과 같다.
resource: file:/D:/%ec%9e%91%ec%97%85%ec%8b%a4/Java/JavaExample/bin/test/
resources:
file:/D:/%ec%9e%91%ec%97%85%ec%8b%a4/Java/JavaExample/bin/
Class.getResource() 메소드의 경우
해당 클래스가 포함된 패키지에 따른 디렉토리를 현재 디렉토리로 함을 알 수 있다.
반면, ClassLoader.getResources() 메소드의 경우
클래스패스의 루트를 현재 디렉토리로 함을 확인할 수 있다.
출처 - http://devday.tistory.com/entry/ClassgetResource-vs-ClassLoadergetResourcegetResources
'Language > JAVA' 카테고리의 다른 글
serialVersionUID 용도 (0) | 2014.11.14 |
---|---|
How to get file resource from Maven src/test/resources/ folder in JUnit test? (0) | 2014.11.14 |
[Apache Commons]Configuration (0) | 2014.11.11 |
pageContext 내장 객체 (0) | 2014.11.11 |
http://www.docjar.com/ (0) | 2014.11.11 |