mvn spring-boot:run vs java -jar
Clash Royale CLAN TAG#URR8PPP
mvn spring-boot:run vs java -jar
I know it may sound silly question but I am unable to understand the difference between mvn spring-boot:run
and java -jar
(.jar file generated with mvn install)
mvn spring-boot:run
java -jar
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.
/src/main/resources/META-INF/resources/WEB-INF/
mvn spring-boot:run
java -jar
The application that I am working on is at https://github.com/ArslanAnjum/angularSpringApi
UPDATE:
It works with spring boot 1.4.2.RELEASE while I intend to use the latest version i.e., 1.5.8.RELEASE.
UPDATE:
Well I solved the problem by putting jsps in src/main/webapp/WEB-INF/views/ and changing packaging type to war and then running this war using java -jar target/myapp.war and its working fine now.
I have shared github link. you can clone the repo.
– ArslanAnjum
Nov 13 '17 at 8:37
Did you take your configuration from some sample? I don't think JSP in a JAR (so embedded container) will work with Tomcat
– Tome
Nov 13 '17 at 10:06
2 Answers
2
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.
spring-boot:run
java -jar
spring-boot:run
test-compile
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 -jar
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:
mvn spring-boot:run
test-compile
resources:resources
compiler:compile
resources:testResources
compiler:testCompile
target/classes
fork
agent
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)
mvn spring:boot
target/classes
target/classes/META-INF/resources/WEB-INF
META-INF/resources/WEB-INF
java -jar
src/main/resources
resources:resources
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/
java -jar
javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/
I understand. But I am unable to make it functional. can specify what to include after $CLASSPATH ? I have tried /classes/resources/META-INF and META-INF/resources.
– ArslanAnjum
Nov 13 '17 at 11:04
Typically it would be
./target/classes
if you are running the command from the root directory of your project to include the target folder. But that's just an example: the important thing is to make sure that all required dependencies and resources are on the classpath. If everything is packaged in your JAR, then you're fine.– Pierre B.
Nov 13 '17 at 17:10
./target/classes
jsps are present in jar. I tried with spring boot 1.4.2 and it is working. May be its a bug with current version of spring boot. ( 1.5.8)
– ArslanAnjum
Nov 14 '17 at 6:39
That's possible! Or a change in the way things are loaded between 1.5.x and 1.4.x.
– Pierre B.
Nov 14 '17 at 8:36
I solved it by changing packaging type to war and putting all the jsps in src/main/webapp/WEB-INF/views. now its runnable via command java -jar target/myapp.war
– ArslanAnjum
Nov 14 '17 at 8:59
Have you tried creating a jar file using mvn package
instead of mvn install
when you are running jar file using java -jar
? package
will create a jar/war as per your POM file whereas install
will install generated jar file to the local repository for other dependencies if present.
mvn package
mvn install
java -jar
package
install
This is not an answer.
– Mehraj Malik
Nov 13 '17 at 9:55
It all depends on the way, the jar is created. This page provides the complete information as how the executable jar or war archives are created using Spring Boot.
– amdg
Nov 13 '17 at 10:10
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
can you post your spring application config file?
– Jobin Joseph
Nov 13 '17 at 7:25