fbpx

Best ways to run spring boot app via command line

We can easily build spring boot application using mvn spring-boot:run and run spring boot app via command line . Assuming you have already created a simple spring boot app, here are the next steps you need to follow to run spring boot app .

Add Maven Plugin in Your Spring Boot Project

In order to run your spring boot app , we need to add spring-boot-maven-plugin in your project parent pom.xml. Open your project pom.xml and add the following line inside it . In most of the cases it will be present already .

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Running Spring boot apps as Jar from Command line

In order to run your spring boot app as jar we need to first build our application . Navigate to your project folder and run the following command to build your app.

mvn clean install 

Now that your project is build successfully , navigate to target directory and locate your spring boot app jar . You jar file name would be something like webapp-1.0.0SNAPSHOT.jar where webapp is the artifactid . Now issue the following command

java -jar webapp-1.0.0.SNAPSHOT.jar 

Running Spring boot based app with profiling enabled

We can also do profiling in our spring boot based app and run the jar in linux . Have a look at How We can enable Profiling in spring boot app.

java -jar -Dspring.profiles.active=dev webapp-1.0.0.SNAPSHOT.jar 

We can also set server port in spring boot app from command line using following command

java -jar -Dspring.profiles.active=dev -Dserver.port=9090 webapp-1.0.0.SNAPSHOT.jar  

Running Spring boot app via command line using maven

Now , I personally really love this command here , reason is it proved to me as Mr Dependable for me . I was getting a some weired issue like my tomcat used to stop without showing any error . I used to use this command and it used to show me the exact error in debug mode .

I was basically having issue in autowiring and i disabled my debug mode by mistake . I found that this actually runs in debug mode for me .

mvn spring-boot:run

This command line run will also be beneficial when you’re using spring-boot-dev-tool with auto reload/restart when you have changed you application.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <optional>true</optional>
</dependency>

How to Run spring Boot App Using Gradle

If you are using gradle as your build tool to build spring boot app , you can try following code to run spring boot app using gradle

./gradle bootRun

How to provide hot fix in spring boot apps

lets take an example where we want to provide hot-fix in spring boot apps . Let’s say we missed a logic in one of our classes and now we have fixed , but deployment and build takes time .

In those scenarios if you have a spring boot app packaged as jar . We need to follow these steps .

  1. Extract the Jar using following command
Jar -xvf webapp-1.0.0.SNAPSHOT.jar

2.Once you extract the jar you will find following structure

spring boot extract jar

Now , you can update your hotfix class file directly , most of the compiled classes are present inside BOOT-INF/classess including your application.properties.

Now you can run following command in spring boot to run this extracted spring boot jar .

java -Dspring.profiles.active=dev -Xdebug -Xnoagent -Djava.compiler=NONE org.springframework.boot.loader.JarLauncher 

Notice we mention profiles as well, if you are not sure . Learn how can you profile your spring boot apps.

These were the top best ways you can run spring boot app from command line .

Have a look at other tutorials on Spring :-

Configure SSL Spring Boot 2

Learn Spring Security