Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot classpath

In the Spring Boot's docs here, about serving static content, it says:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.

I found that all the content in the directory:

src/main/resources 

will be copied inside the classpath, so I can put my static content in:

src/main/resources/static 

and all will work fine and I'm happy since I can have my static content under the src directory.

But, I have some questions about this:

  1. Why the documentation doesn't say to put static content in src/main/resources/static instead of speaking about the classpath (I think this is a bit confusing)?
  2. Is it good to assume that the content in src/main/resources/ will be always copied in the classpath?
  3. Is there some Spring Boot official documentation explaining what I'm supposed to find in the classpath other than Java classes and packages (up to now I only know I can found all the content from src/main/resources/)?
like image 836
Andrea Avatar asked Dec 08 '15 16:12

Andrea


People also ask

What is the spring boot classpath?

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.

What is classpath used for?

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

How do I find my classpath?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

What is the default classpath?

The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.


2 Answers

/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.

like image 186
Sotirios Delimanolis Avatar answered Sep 20 '22 19:09

Sotirios Delimanolis


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.

like image 38
dunni Avatar answered Sep 22 '22 19:09

dunni