Introduction
In this chapter, we will learn how to run the Spring Boot application from the command line. Running your application from the command line can be useful for testing, deployment, and automation purposes.
Prerequisites
Before you can run your Spring Boot application from the command line, ensure that you have the following installed and properly configured on your system:
- Java Development Kit (JDK): Ensure that Java is installed and the
JAVA_HOMEenvironment variable is set. - Apache Maven: Ensure that Maven is installed and the
M2_HOMEenvironment variable is set.
Steps to Run Spring Boot Application from Command Line
1. Package the Application
First, you need to package your Spring Boot application into an executable JAR file. You can do this using Maven.
- Navigate to your project directory:
cd path/to/your/project
- Package the application using Maven:
mvn clean package
This command will compile your code, run tests, and package your application into a JAR file located in the target directory.
2. Run the Executable JAR
Once your application is packaged, you can run it using the java -jar command.
- Navigate to the target directory:
cd target
- Run the JAR file:
java -jar thymeleaf-helloworld-0.0.1-SNAPSHOT.jar
Replace thymeleaf-helloworld-0.0.1-SNAPSHOT.jar with the name of the JAR file generated by Maven.
3. Verify the Application
- Open a web browser:
- Go to
http://localhost:8080/hello. - You should see the message “Hello, World!”.
- Go to
Optional: Running with Custom Configuration
You can also run your Spring Boot application with custom configuration properties by specifying them in the command line.
Example: Changing the Server Port
java -jar thymeleaf-helloworld-0.0.1-SNAPSHOT.jar --server.port=8081
This command will start your Spring Boot application on port 8081 instead of the default port 8080.
Conclusion
In this chapter, you have learned how to run your Spring Boot application from the command line. We covered packaging the application using Maven, running the executable JAR file, and customizing configuration properties through command line arguments. Running your application from the command line is a useful skill for testing, deployment, and automation.