Spring Boot classpath
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The classpath is one of those Java concepts that feels invisible until something fails to load. In Spring Boot, understanding the classpath helps you explain why dependencies are discovered automatically, why application.properties is found without extra wiring, and why a packaged jar behaves differently from a plain Java project.
What the Classpath Means in Spring Boot
At the JVM level, the classpath is the set of directories and jar files used to locate classes and resources. Spring Boot builds on that mechanism. When your application starts, the JVM and Spring both search the classpath for:
- compiled application classes
- dependency jars
- configuration files
- templates and static assets
In a standard Maven or Gradle project, anything under src/main/java is compiled into classes, and anything under src/main/resources is copied onto the runtime classpath.
That is why a file like src/main/resources/application.properties is available automatically at startup.
Loading Classpath Resources
Spring provides several ways to read files from the classpath. The most explicit is ClassPathResource.
For this to work, messages.txt should live in src/main/resources.
Spring also supports the classpath: prefix in places where a resource location is configured:
The classpath: prefix makes your intent explicit and avoids mixing file system paths with packaged resources.
How Dependencies Reach the Classpath
Dependencies declared in Maven or Gradle become part of the application classpath according to their scope or configuration.
A simple Maven example:
A similar Gradle example:
The important detail is that not every dependency is available in every phase. A testImplementation dependency is on the test classpath, not the main runtime classpath. That distinction explains many ClassNotFoundException problems.
Why Spring Boot Fat Jars Feel Different
A packaged Spring Boot jar is not laid out like a plain Java jar with all classes flat at the top level. Boot creates an executable archive that contains:
- your compiled classes
- your resource files
- dependency jars nested inside the archive
When you run java -jar app.jar, Spring Boot uses its launcher to assemble the effective runtime classpath from those nested jars. That is why a resource that works in your IDE usually still works inside the packaged artifact, as long as it was added to src/main/resources and not referenced through a hardcoded file path.
Common Resource Patterns
Two patterns appear in most Boot applications.
Reading a configuration or seed file:
Referencing a SQL script in configuration:
Both examples rely on the resource being packaged on the classpath rather than existing as an external file beside the jar.
Common Pitfalls
The most common mistake is using a file system path for something that should be loaded from the classpath. Code that points to src/main/resources/... may work in the IDE and then fail once the application is packaged, because those source directories do not exist in production.
Another issue is placing files in the wrong source folder. If a resource is saved under src/test/resources, it will be available during tests but missing at runtime.
Dependency scopes also cause confusion. If a library is declared only for tests, your application compiles in the wrong environment and then fails at startup with missing classes.
Finally, developers sometimes assume Spring Boot scans every package automatically. Component scanning starts from the package of the main application class. That is related to classpath visibility but not the same thing. A class can be present on the classpath and still not be discovered as a Spring bean if it is outside the scan path.
Summary
- The classpath is the runtime search path for classes and resource files.
- In Spring Boot,
src/main/resourcesis copied onto the application classpath. - Use
ClassPathResourceor theclasspath:prefix to load packaged resources. - Dependency scope determines whether a library is available at runtime, test time, or both.
- Avoid hardcoded source-directory paths because packaged Spring Boot jars load resources differently.
Related reading
- Spring Boot Cloud Zuul Proxy 404 Error
- Spring boot config server
- Spring Boot Configuration Class is simply ignored and not loaded
- Spring Boot configure and use two data sources
- Spring Boot configure and use two data sources
- Spring boot Configure your tomcat server to work with html5Mode
- Spring Boot containers can not connect to the Kafka container
- Spring boot context load test hangs

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.