Spring Boot application - difference in startup time with "mvn spring-boot:run" and "java -jar"

Viewed 1179

I noticed a big difference in the startup time of my Spring Boot application when starting with Maven and as jar. For example:

  • mvn spring-boot:run - 5 seconds
  • java -jar myapp.jar - 25 seconds

Starting the jar file takes like 5 times longer. Why is it so? What is happening in the background when starting the application with Maven and as jar? Is something loaded differently? And is it possible to start the jar file for the time used by Maven?

1 Answers

This difference could be due to the fact that spring-boot:run is actually running the Spring Boot application inside Maven JVM (Unless you explicitly set the fork argument), When it does run inside Maven JVM it is actually running as a new Thread not a Process. Creating a Thread is much faster than a Process.

But java -jar command will create a OS Process, Process creation has steps attached to it like requesting a process id, allocating memory, etc. On top of that the myapp.jar will need to extracted plus the JVM will not have any optimization as it will be reading the .class extracted from the jar file for the first time. Basically a Cold Start which takes time.

You can see the source code for Maven Spring Boot Plugin here

Related