Spring Boot is a popular framework for building web applications in Java. One of the advantages of using Spring Boot is that it makes it easy to run your application from the command line. In this blog post, we’ll walk through the process of running a Spring Boot application through the command line.
Setup
Before we begin, make sure you have the following installed on your machine:
- Java 8 or later
- Spring Boot CLI (Command Line Interface)
- A text editor or IDE
Using Maven command
Assuming you already have a Spring Boot application ready to go, open up a command prompt or terminal window and navigate to the root directory of your application. From there, you can use the following command to run your application:
./mvnw spring-boot:run
This command uses the Maven Wrapper (mvnw) to run the Spring Boot plugin, which in turn starts your application.
Using Gradle command
If you’re using Gradle, you can use the following command instead:
./gradlew bootRun
Console Output
You should see the following output in the command prompt or terminal window:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-09-01 12:34:35.123 INFO 1234 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v1.0.0 on localhost with PID 1234
2021-09-01 12:34:35.123 INFO 1234 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-09-01 12:34:35.456 INFO 1234 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-09-01 12:34:35.789 INFO 1234 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2021-09-01 12:34:36.012 INFO 1234 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-09-01 12:34:36.234 INFO 1234 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.234 seconds (JVM running for 5.678)
The output above shows that the application has started successfully and is running on port 8080. You can now open up a web browser and navigate to http://localhost:8080
to see your application in action.
You can also pass command-line arguments to your application when you run it
Command line spring boot arguments
Additionally, you can also pass command-line arguments to your application when you run it. These arguments can be used to configure various aspects of the application, such as the port it runs on or the active profile. Here’s an example of running the application on port 8888 with the ‘production’ profile:
Copy code./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=8888,--spring.profiles.active=production
In this example, we’re passing two arguments to the application: --server.port=8888
and --spring.profiles.active=production
. The first argument sets the port that the application will run on, while the second sets the active profile.
Command Line Java arguments
You can also pass JVM arguments when running the application by using -D
instead of --
in the command line arguments. For example, you can set the maximum heap size for the JVM like this:
Copy code./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=8888 -Djava.net.preferIPv4Stack=true -Xmx256m
In this example, we are setting the maximum heap size to 256MB, and setting java.net.preferIPv4Stack
to true.
Conclusion
In conclusion, running a Spring Boot application through the command line is a simple and convenient way to start your application. By using the command line, you can easily pass arguments to configure your application and control how it starts up. With this knowledge, you will be able to run your Spring Boot applications with ease and flexibility.