Running a Spring Boot application from the command line is pretty easy. Let’s see how to do it.
If you are running a Maven project, you can build and run a Spring Boot application from the command line by adding the following maven plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then, you can build your application as follows:
mvn install
Now you have two options to run the application from the command line:
java -jar target/app-0.0.1-SNAPSHOT.jar
or
mvn spring-boot:run
Both commands, will run the Main class in your project which contains the annotation @SpringBootApplication:
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
In case your Spring Boot project contains more than one class, you can specify the main class as system property:
java -cp app.jar -Dloader.main=com.acme.MyAppMain2 org.springframework.boot.loader.PropertiesLauncher
Or you can also configure the main class in Maven pom.xml:
<properties> <start-class>com.acme.MyAppMain2</start-class> </properties>
Finally, you can configure main class for spring-boot-maven-plugin:
<?xml version="1.0" encoding="UTF-8"?><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
On the other hand, if you are using Gradle to build your Spring Boot projects,you can use the gradle command to run the Main class:
gradle bootRun
Found the article helpful? if so please follow us on Socials