Classpath

2024. 10. 16. 10:40Spring Boot/Spring Boot Auto Configuration

🔍 클래스패스(classpath)란 무엇인가?

✅ 한 문장 요약

클래스패스(ClassPath)는 JVM이 .class 파일(또는 리소스 파일)을 찾기 위해 참조하는 경로들의 목록입니다.
이 경로는 디렉터리, .jar 파일, .zip 파일 형태로 존재할 수 있으며, JVM은 여기서 클래스나 리소스를 찾아 필요할 때 로드합니다.

 

📦 1. 클래스패스 = 파일 시스템의 경로 목록

java -cp "lib/*:config:classes" com.example.Main
  • lib/*: 모든 JAR 파일 포함
  • config: 설정 파일 위치
  • classes: 컴파일된 .class 파일 디렉터리

➡ 이 문자열이 JVM에 전달되는 **클래스패스(경로 목록)**입니다.

📌 이 클래스패스는 OS 파일 시스템의 경로이며, 실제 존재하는 디렉터리 또는 파일이어야 합니다.

 

🛠️ 2. JVM에서 클래스패스를 지정하는 방법

2.1 명령줄에서 직접 지정

java -cp classes:lib/* com.example.App
 
 

2.2 CLASSPATH 환경 변수로 지정 (비권장)

export CLASSPATH=/my/app/classes:/my/app/lib/*
 
권장 방식은 아님. -cp나 -classpath 옵션을 직접 사용하는 것이 더 명확함.

 

2.3 Spring Boot의 경우

Spring Boot는 자체적으로 LaunchedURLClassLoader를 사용해
myapp.jar 내의 BOOT-INF/classes/와 BOOT-INF/lib/*.jar를 동적으로 클래스패스에 추가합니다.

// java -jar myapp.jar → 내부적으로 이 위치들이 classpath 역할 수행
BOOT-INF/
├── classes/  ← 앱의 컴파일 결과 (소스)
└── lib/      ← 디펜던시 .jar 파일들

 

📚 3. 클래스패스의 목적은?

클래스패스는 단순히 JVM에게 **“클래스를 어디서 찾을까?”**를 알려주는 경로 리스트입니다.

찾는 대상은?

  • .class 파일 (컴파일된 자바 클래스)
  • .properties, .xml, .yml, .conf 등 리소스 파일

사용 예

  • Class.forName("com.example.MyClass") → 클래스패스 경로 안에서 MyClass.class를 찾음
  • ClassLoader.getResourceAsStream("config/app.properties") → 클래스패스 경로에서 해당 리소스를 탐색

 

⚙️ 4. JVM은 클래스패스를 어떻게 활용하는가?

클래스 로딩 시, JVM은 다음과 같은 순서로 클래스를 찾습니다:

 

4.1 ClassLoader 계층 구조

BootstrapClassLoader     (JDK 내장 클래스, rt.jar)
      ↓
PlatformClassLoader      (JDK module system)
      ↓
AppClassLoader           (classpath에 명시한 .jar, .class)
 

4.2 AppClassLoader가 하는 일

  • java -cp로 지정한 경로에서 .class 파일을 찾음
  • 찾으면 로딩 → Class<?> 객체 생성
  • 못 찾으면 ClassNotFoundException 발생

 

🧠 5. 클래스패스의 실체는 ClassLoader가 관리

JVM은 내부적으로 다음과 같은 방식으로 classpath를 관리합니다:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (URL url : urls) {
    System.out.println(url);
}
 

➡ 실제로는 classpath에 포함된 디렉터리, jar 파일 경로가 URL로 관리됩니다.

 

💡 6. Spring Boot에서는 어떻게 다르나?

Spring Boot는 spring-boot-loader를 통해 **자체 ClassLoader (LaunchedURLClassLoader)**를 사용합니다.

왜?

spring-boot-app.jar 하나만으로 실행되도록 하되,
내부 BOOT-INF/lib/*.jar에 있는 클래스도 classpath처럼 인식시켜야 하므로.

동작 방식

// spring-boot-loader
LaunchedURLClassLoader extends URLClassLoader {
    // BOOT-INF/classes, BOOT-INF/lib/*.jar을 동적으로 classpath에 추가
}

➡ Spring Boot는 “클래스패스의 확장 버전”을 자체 구현한 것.

 

✅ 정리

항목 설명
클래스패스란? JVM이 .class나 .properties 등을 찾기 위해 참조하는 경로 목록
클래스패스의 실체 디렉터리, .jar, .zip 경로들 (OS 파일 시스템 경로)
JVM에서 역할 클래스 로딩 시 탐색 위치로 사용됨
Spring Boot의 방식 LaunchedURLClassLoader로 내부 JAR를 classpath처럼 처리
메모리 로딩 여부 모든 경로는 등록만 될 뿐, 실제 클래스는 사용될 때 로딩됨

 

🔍 Spring Boot 프로젝트의 classpath

 

Spring Boot 프로젝트의 classpath를 확인하기 위해 아래 그림처럼 STS4 IDE에서 Maven Build를 선택합니다.

 

그리고 Goals 입력창에 dependency:tree를 입력하고 Run 버튼을 선택합니다.

 

STS4 콘솔창에서 현재 프로젝트의 classpath를 확인할 수 있습니다.

mvn dependency:tree 명령문 실행 결과입니다.

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< sia:taco-cloud >---------------------------
[INFO] Building taco-cloud 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.5.0:tree (default-cli) @ taco-cloud ---
[INFO] sia:taco-cloud:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:3.1.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:3.1.2:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:3.1.2:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.4.8:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.4.8:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.20.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.20.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:2.0.7:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.33:compile
[INFO] |  \- org.thymeleaf:thymeleaf-spring6:jar:3.1.1.RELEASE:compile
[INFO] |     +- org.thymeleaf:thymeleaf:jar:3.1.1.RELEASE:compile
[INFO] |     |  +- org.attoparser:attoparser:jar:2.0.6.RELEASE:compile
[INFO] |     |  \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
[INFO] |     \- org.slf4j:slf4j-api:jar:2.0.7:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.1.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:3.1.2:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.15.2:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.2:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.15.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.15.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.15.2:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.15.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.1.2:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.11:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.11:compile
[INFO] |  +- org.springframework:spring-web:jar:6.0.11:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:6.0.11:compile
[INFO] |  |  \- io.micrometer:micrometer-observation:jar:1.11.2:compile
[INFO] |  |     \- io.micrometer:micrometer-commons:jar:1.11.2:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:6.0.11:compile
[INFO] |     +- org.springframework:spring-aop:jar:6.0.11:compile
[INFO] |     +- org.springframework:spring-context:jar:6.0.11:compile
[INFO] |     \- org.springframework:spring-expression:jar:6.0.11:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:3.1.2:runtime
[INFO] |  +- org.springframework.boot:spring-boot:jar:3.1.2:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:3.1.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:3.1.2:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.11:compile
[INFO] |  \- org.hibernate.validator:hibernate-validator:jar:8.0.1.Final:compile
[INFO] |     +- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
[INFO] |     +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:compile
[INFO] |     \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] +- org.projectlombok:lombok:jar:1.18.28:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:3.1.2:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:3.1.2:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.1.2:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.8.0:test
[INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.0:test
[INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:2.1.2:test
[INFO] |  +- net.minidev:json-smart:jar:2.4.11:test
[INFO] |  |  \- net.minidev:accessors-smart:jar:2.4.11:test
[INFO] |  |     \- org.ow2.asm:asm:jar:9.3:test
[INFO] |  +- org.assertj:assertj-core:jar:3.24.2:test
[INFO] |  |  \- net.bytebuddy:byte-buddy:jar:1.14.5:test
[INFO] |  +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.9.3:test
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.3:test
[INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.9.3:test
[INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.9.3:test
[INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.9.3:test
[INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.9.3:test
[INFO] |  +- org.mockito:mockito-core:jar:5.3.1:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.14.5:test
[INFO] |  |  \- org.objenesis:objenesis:jar:3.3:test
[INFO] |  +- org.mockito:mockito-junit-jupiter:jar:5.3.1:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.1:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:6.0.11:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:6.0.11:compile
[INFO] |  +- org.springframework:spring-test:jar:6.0.11:test
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.1:test
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.1.2:compile
[INFO] |  +- com.zaxxer:HikariCP:jar:5.0.1:compile
[INFO] |  \- org.springframework:spring-jdbc:jar:6.0.11:compile
[INFO] |     \- org.springframework:spring-tx:jar:6.0.11:compile
[INFO] \- com.h2database:h2:jar:1.4.196:runtime
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.424 s
[INFO] Finished at: 2024-01-31T13:50:06+09:00
[INFO] ------------------------------------------------------------------------

 

참고 : https://sundaland.tistory.com/384

 

'Spring Boot > Spring Boot Auto Configuration' 카테고리의 다른 글

@EnableAutoConfiguration  (0) 2024.10.16
@SpringBootConfiguration  (0) 2024.10.16
SpringApplication.run  (0) 2024.10.16
@SpringBootApplication  (0) 2024.10.16
@Import  (0) 2023.05.04