자바 클래스 로딩 메커니즘에 대한 간단한 질문이 있습니다.클래스 경로로 자바 클래스 로딩
기본 클래스 로더가 사용자 정의 클래스를로드한다고 생각합니다. 클래스 패스에 다른 jar를 지정하면 기본 클래스 로더가 응용 프로그램 시작시 각 jar를 통과하고 각 jar의 클래스를로드합니까?
자바 클래스 로딩 메커니즘에 대한 간단한 질문이 있습니다.클래스 경로로 자바 클래스 로딩
기본 클래스 로더가 사용자 정의 클래스를로드한다고 생각합니다. 클래스 패스에 다른 jar를 지정하면 기본 클래스 로더가 응용 프로그램 시작시 각 jar를 통과하고 각 jar의 클래스를로드합니까?
아니요, 처음 참조 할 때마다 Class.forName()
을 통해 또는 코드에서 직접 사용하여 클래스를로드합니다.
예 :
public class First {
static {
System.out.println("first");
}
public static void main(final String[] args) {
System.out.println("second");
Second.third();
}
}
public class Second {
static {
System.out.println("third");
}
public static void third() {
System.out.println("fourth");
}
}
당신은 메인 클래스로 First
를 실행하는 경우, 출력은 다음과 같습니다
first <-- First is loaded
second <-- method in First is executed
third <-- Second is loaded
fourth <-- Method in Second is executed
클래스 패스에 정의 된 10 개의 jar가 있다고 가정 해 봅시다. 그리고 런타임시 클래스 패스에서 7 번째 jar에 정의 된 Test 객체가 필요하면 첫 번째 항아리에서 클래스 객체를 찾기 시작합니까? – user826323
@ user826323 예, classpath 요소 (jar 또는 폴더)를 classpath에 표시된 순서대로 검색합니다 –
가능한 중복 : http://stackoverflow.com/questions/6266156/does-system -classloader-load-all-classes-in-classpath-even-theyre-not-actuall – stivlo
내 프로젝트에는 약 250 개의 병이 있습니다. 시작할 때 모든 항아리의 모든 클래스를로드하면 울게됩니다. – MarianP