/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.
I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.
The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.
Answer from Sotirios Delimanolis on Stack Overflow/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.
I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.
The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.
The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources. So if the documentation would only state the folder src/main/resources/static, it would be incomplete.
Ad 2: As long as you don't mess with the default Maven configuration, then it's safe to assume this.
Ad 3: Maybe start with the official Oracle documentation: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html. Hint: Of course, it's not only the contents of the resources folder, which are in the classpath, but also of course all compiled classes, hence its name.
What is the classpath? How can I console it? (Spring project)
java - what's the difference between classpath and classpath* in spring boot? - Stack Overflow
java - Classpath file directory root (.classpath) in Spring-Boot project - Stack Overflow
java - How to configure additional classpath in SpringBoot? - Stack Overflow
Videos
I'm trying to debug something (Spring Application) and I feel that it might have something to do with the classpath. Honestly, I barely understand this concept so I have no idea.
I know that this concept is independent of Spring, but how does Spring define the classpath? If I run mvn spring-boot:run, for example.
Is there a way to console it (ie System.out.println('myclasspath')?
I tried googling this these things but i could not find a clear answer. I'm new to Java/Spring but it seems that most documentation assumes that the developer understands these things.
From Spring documentation
The wildcard classpath relies on the getResources() method of the underlying classloader. As most application servers nowadays supply their own classloader implementation, the behavior might differ especially when dealing with jar files. A simple test to check if classpath* works is to use the classloader to load a file from within a jar on the classpath: getClass().getClassLoader().getResources(""). Try this test with files that have the same name but are placed inside two different locations. In case an inappropriate result is returned, check the application server documentation for settings that might affect the classloader behavior.
So classPath is for load the resources from current class loader (simply for understanding will not read resources under jar or other project dependency)
classpath* will do the jar or other class loader resources.
First since Spring boot show banner in source.You can find the code in SpringApplication.class, printBanner method is used for to show banner.Like mallikarjun said classPath is for load the resources from current class loader (simply for understanding will not read resources under jar or other project dependency) classpath* will do the jar or other class loader resources. If you use classpath*:banner.txt will find in the jar.
You can use the loader.path parameter to define a location for an external lib folder. All jars under this folder will be added to the classpath. For example, if you'd like to define C:\extLib as your external lib folder, you can do the following:
java -Dloader.path=/C:/extLib/ -jar aapName.jar
For this to work, you need to use the PropertiesLauncher. There are two ways to do that:
Option 1
Update the project pom.xml and add the following tag:
<configuration> <!-- added -->
<layout>ZIP</layout> <!-- to use PropertiesLauncher -->
</configuration>
Effective build tag, the post-update looks like below:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!-- added -->
<layout>ZIP</layout> <!-- to use PropertiesLauncher -->
</configuration>
</plugin>
</plugins>
</build>
Option 2
Use the PropertiesLauncher when launching the application from the commandline:
java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher
References:
How to add jars to SpringBoot classpath with jarlauncher
You may refer this below link from spring boot:
https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features
You can use the loader.path property to define a lib folder location
Does
classpath:search for resource relative to the document in which it is specified(in case of web applications)?
No, classpath: is always relative to the classpath root. If you put a / at the start of the path, it is silently removed.
Is it more fast to search if i give direct location of resource instead e.g.
classpath:/WEB-INF/classes/myfolder/myfile.txt
No, that won't work at all. The classpath root contains /WEB-INF/classes, so the path should be relative to that.
Don't confuse classpath: paths with file paths, they have no relation to each other.
Take a look at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-classpath-wildcards
This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(...) call), and then merged to form the final application context definition.
So classpath: starts at the root of your classpath.