Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 350
Anna Avatar asked Nov 08 '17 10:11

Anna


People also ask

What happens when a spring boot application is run as Java application?

What happens in the background when a Spring Boot Application is “Run as Java Application”? Ans: If you are using Eclipse IDE, Eclipse maven plugin ensures that as soon as you add a dependency or make a change to the class file, it is compiled and ready in the target folder!

What is the difference between Java application and spring boot application?

What is the difference between Java and Spring? Java is a programming language, while Spring is an open source application framework. Therefore, they cannot be directly compared. However, Java EE (which is Java's own server programming platform) is often compared against Spring framework.

What is the difference between Maven and Spring boot?

Spring is used for dependency injection. Maven is used to supply dependencies.

Which method will run when the application starts from the beginning?

boot. In startup process after the context is initialized, spring boot calls its run() method with command-line arguments provided to the application.


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

like image 162
shazin Avatar answered Oct 20 '22 16:10

shazin