SoFunction
Updated on 2025-04-09

Interpretation of classpath usage in SpringBoot

Detailed explanation of classpath in SpringBoot

Understand when developing Spring Boot applicationsclasspathThe concept of configuring, resource management, and building projects is very important. Especially when we use the Maven packaging tool, the project's resource files will be placed in different directories at different stages.

This article will explore in-depth about Spring Bootclasspathand how related resources are managed, especially when we use Maven to build, how resource files are from the development environmentresourcesMove the directory to the final JAR packageBOOT-INF/classesTable of contents.

1. What is classpath?

classpathIs the path used by Java programs to find classes and resource files. It specifies the path to which the JVM loads classes and resources at runtime.

Spring Boot applications, especially when built with Maven,classpathPlays a crucial role because it includes not only Java class files, but also other resource files, such as configuration files, static files, template files, etc.

2. Resource Management in Spring Boot

In Spring Boot, all resource files, such as, static files (e.g..html, .js, .css) and template files (e.g..ftl.thymeleafetc.) can be placed insrc/main/resourcesIn the directory.

2.1 Storage of resource files in the development stage

During the development stage, Spring Boot uses Maven build tools, and the resource files in the project are generally stored insrc/main/resourcesIn the directory.

src/main/resourcesDirectory structure

src
└── main
    └── resources
        ├── 
        ├── static
        │   ├── js
        │   └── css
        ├── templates
        │   └── 
        └── other-files
            └── 

During this stage, Spring Boot will load directlysrc/main/resourcesFiles in the directory, no additional configuration is required.

Spring Boot Automatic ScanresourcesFiles in the directory and use them asclasspathThe resources in it are loaded.

2.2 Compilation stage

When we build a project using Maven, the resource file is copied totarget/classesIn the directory.

MavenresourcesPlugins to process these resources and ensure that all resource files are transferred fromsrc/main/resourcesCopy totarget/classesTable of contents.

This directory is what we call the classpath (classpathpart of ).

target/classesDirectory structure

target
└── classes
    ├── 
    ├── static
    │   ├── js
    │   └── css
    ├── templates
    │   └── 
    └── other-files
        └── 

At this time, Spring Boot's application can already be directly fromtarget/classesThe directory loads the resource file.

3. Package as a JAR file

When building a Spring Boot project using Maven package, the final output is a JAR file containing all dependencies and resources.

Spring Boot uses the Maven plugin to create an executable JAR file that contains all the project's.classFiles and resource files.

3.1 Directory structure in JAR file

The JAR file created by Spring Boot is a self-contained file that contains all the necessary components of the application, and the resource file will be placed inBOOT-INF/classesIn the directory.

This is different from traditional JAR files, which place resource files directly in the root directory, while Spring Boot will place all resource files inBOOT-INF/classesand maintain the original directory structure.

targetJAR file structure in directory

target
└── 
    ├── BOOT-INF
    │   └── classes
    │       ├── 
    │       ├── static
    │       │   ├── js
    │       │   └── css
    │       ├── templates
    │       │   └── 
    │       └── other-files
    │           └── 
    ├── META-INF
    └── org
        └── springframework
            └── boot
                └── loader

In this structure, Spring Boot's resource files are organized inBOOT-INF/classesIn the directory. All resource files, including, static resources and template files are all located in this directory.

Maven will transfer these files fromtarget/classesCopy the directory to the JAR packageBOOT-INF/classesTable of contents.

3.2 BOOT-INF/classes directory

The internal structure of Spring Boot's JAR package includes:

  • BOOT-INF/classes: This is the directory where the application's class and resource files are located. Spring Boot will load all classes and resources from this directory.
  • BOOT-INF/lib: This directory contains all the application's dependencies JAR files, which Spring Boot will load as application's dependencies.
  • META-INF: This directory contains metadata of JAR files, such as MANIFEST files, Spring Boot launcher configuration, etc.

4. Maven configuration

Maven usesspring-boot-maven-pluginPlugin to create executable JAR files.

This plug-in automatically handles the generation process of JAR file and correctly places the application's class and resource files in the corresponding location of the JAR file.

Example:

<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Other dependencies -->
</dependencies>

<build>
    <plugins>
        <!-- Spring Boot Maven Plugin -->
        <plugin>
            <groupId></groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

In Maven,spring-boot-maven-pluginCopy of resource files will be processed automatically and placed correctly inBOOT-INF/classesIn the directory. It ensures that all necessary files are included in the JAR file so that the application can run correctly.

5. Run the JAR file

When we executejava -jar Spring Boot will use it when starting a Spring Boot applicationBOOT-INF/classesResource files and classes in the directory to start the application.

Spring Boot startup process

  • Spring Boot will automatically recognize the JAR packageBOOT-INF/classesdirectory and load it as classpath.
  • Configuration files, static resources and template files are all fromBOOT-INF/classesload in to ensure that the application's resources are loaded correctly.

6. Resource loading mechanism

Spring Boot loads resources in the JAR file through the class loader at startup.

If you need to access resources, Spring Boot will depend on Spring'sResourceLoaderTo provide the rightclasspathaccess support for resources in.

@Autowired
private ResourceLoader resourceLoader;

public void loadFile() throws IOException {
    Resource resource = ("classpath:");
    InputStream inputStream = ();
    // Read file content}

ResourceLoaderpassclasspath:Prefix to access resources under the classpath.

Summarize

In Spring Boot,classpathIt is a key part of the storage and access of resource files in applications. During the development stage, resource files are stored insrc/main/resourcesIn the directory, it is copied totarget/classesin the directory.

When finally packaged as a JAR file, the resource file will be placed inBOOT-INF/classesIn the directory, this is different from the structure of traditional JAR files.

When Spring Boot is built using Maven,spring-boot-maven-pluginCopying and organization of resource files is automatically processed to ensure that they are loaded correctly when the application starts.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.