Short answer: spring-boot:run
is a java -jar
command on steroïd running as part of your Maven build, ensuring all required parameters are passed to your app (such as resources). spring-boot:run
will also ensure that your project is compiled by executing test-compile
lifecycle goals prior to running your app.
Long answer:
When you run java -jar
, you launch a new JVM instance with all the parameters you passed to this JVM. For example, using the Spring doc example
java -Xdebug -Xrunjdwp:server=y, \
transport=dt_socket, address=8000, suspend=
-jar target/myproject-0.0.1-SNAPSHOT.jar
You will launch a brand new JVM with the given parameters. You need to make sure to include everything needed, such as classpath elements, application parameters, JVM options, etc. on the command line.
When you run mvn spring-boot:run
, you launch a Maven build that will:
- Run the
test-compile
lifecycle goals, by default it will beresources:resources
,compiler:compile
,resources:testResources
,compiler:testCompile
goals of the Maven Resources and Compiler plugin. - Launch your application with a bunch of parameters that will depend on the
Spring Boot Maven Plugin configuration you defined in your project (your pom.xml, parents and settings, command line, etc.). This includes among other things:- A lot of classpath elements: your
target/classes
folder which may contain resources and libraries required by your app, your Maven dependencies, etc. - Whether to fork your JVM or not (whether to create a brand new JVM to run your app or re-use the JVM of the Maven build), see
fork
andagent
parameter of the plugin
- A lot of classpath elements: your
As per:
I have a spring boot application with jsp pages in
/src/main/resources/META-INF/resources/WEB-INF/. If I use mvn
spring-boot:run these pages are served. But If I use java -jar these
pages are not found by application.
It’s because the mvn spring:boot
command will make sure your target/classes
folder is present in the Classpath when your app is running. After compilation, this folder will contain target/classes/META-INF/resources/WEB-INF
among other things. Your app will then be able to find META-INF/resources/WEB-INF
and load them when asked. When you ran java -jar
command, this folder was probably not on the classpath, your app was then not able to find your resources. (these resources were copied from the src/main/resources
folder during the resources:resources
goal)
To have a similar result with your java -jar
command, you must include your resources on the classpath such as javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/