Three Ways to Start a Spring Boot Application
This article explains the three primary ways to launch a Spring Boot application—running a class with a main method, using the java -jar command, and leveraging the spring-boot-maven-plugin—along with code examples, configuration details, and Maven command options.
Spring Boot provides three main startup methods: executing a class that contains a main method, running the packaged JAR with java -jar, and using the spring-boot-maven-plugin.
1. Execute a class with a main method
This is the simplest approach, often done directly from an IDE like IntelliJ IDEA, which automatically loads configuration files from the classpath.
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) {
/**
* SpringApplication will automatically load <code>application.properties</code> from several locations:
* 1. <b>/config</b> subdirectory of the current directory;
* 2. The current directory;
* 3. A classpath <code>/config</code> package;
* 4. The classpath root.
*/
SpringApplication.run(Example.class, args);
}
}In IDEA you can also set application parameters via the run configuration.
2. Use java -jar command
java -jar jar_path --param jar_pathrefers to the location of the packaged JAR file, and --param represents command‑line arguments, e.g.: java -jar example.jar --server.port=8081 This overrides the port defined in application.properties.
3. Start via spring-boot-maven-plugin
Add the following plugin configuration to your Maven project (the parent spring-boot-starter-parent already provides the appropriate version):
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- version is inherited from the parent -->
</plugin>After setting up, run the application from the project root: mvn spring-boot:run To pass arguments, consult the plugin’s help: mvn spring-boot:help -Ddetail Then specify arguments using -Drun.arguments, for example:
mvn spring-boot:run -Drun.arguments="--server.port=8888"This command passes the specified parameters to the application at runtime.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
