Running Spring Boot Applications
Now that you've created a basic Spring Boot project, let's explore how to run it. Spring Boot provides several convenient ways to execute your application, catering to different development and deployment scenarios.
1. Running from an IDE (IntelliJ IDEA, Eclipse, VS Code)
This is the most common and convenient method during development. Most popular IDEs have built-in support for Spring Boot.
- IntelliJ IDEA:
- Open your project.
- Locate the main application class (the one with the
@SpringBootApplicationannotation). - Right-click within the class and select "Run 'YourApplicationName'". IntelliJ IDEA will automatically detect the main method and run the application.
- Eclipse:
- Open your project.
- Locate the main application class.
- Right-click within the class and select "Run As" -> "Spring Boot App".
- VS Code:
- Ensure you have the Spring Boot Extension Pack installed.
- Open your project.
- Locate the main application class.
- Right-click within the class and select "Run Java". VS Code will use the Spring Boot extension to run the application.
The IDE will typically start an embedded Tomcat server (or another supported server) and deploy your application. You'll see output in the console indicating the server is starting and eventually that your application is running.
2. Running from the Command Line with Maven
If you're using Maven, you can run your Spring Boot application from the command line using the mvn spring-boot:run command.
Open a terminal or command prompt.
Navigate to the root directory of your Spring Boot project (the directory containing the
pom.xmlfile).Execute the following command:
mvn spring-boot:runMaven will download dependencies (if necessary), compile your code, and start the embedded server. The application's logs will be displayed in the console.
3. Running from the Command Line with Gradle
If you're using Gradle, you can run your Spring Boot application from the command line using the ./gradlew bootRun command.
Open a terminal or command prompt.
Navigate to the root directory of your Spring Boot project (the directory containing the
build.gradleorbuild.gradle.ktsfile).Execute the following command:
./gradlew bootRunGradle will download dependencies (if necessary), compile your code, and start the embedded server. The application's logs will be displayed in the console. (On Windows, use
gradlew bootRuninstead of./gradlew bootRun).
4. Running as a JAR File
Once your application is built, you can package it as an executable JAR file. This is the typical way to deploy Spring Boot applications.
Build the JAR file:
- Maven:
mvn clean package - Gradle:
./gradlew build(orgradlew buildon Windows)
- Maven:
Locate the JAR file: The JAR file will be created in the
targetdirectory (Maven) orbuild/libsdirectory (Gradle). The filename will typically be something likeyour-application-name-1.0.0.jar.Run the JAR file:
java -jar your-application-name-1.0.0.jarThis will start the embedded server and run your application.
Accessing Your Application
After running your application using any of the methods above, you can access it in your web browser or using a tool like curl. By default, Spring Boot applications run on port 8080.
- Example: If your application has a controller that maps requests to
/hello, you can access it by openinghttp://localhost:8080/helloin your browser.
Changing the Port
You can change the default port by setting the server.port property in your application.properties or application.yml file.
application.properties:
server.port=8081
application.yml:
server:
port: 8081
Now your application will run on port 8081, and you can access it at http://localhost:8081/hello.
Summary
You've learned several ways to run your Spring Boot application:
- From an IDE: The easiest method for development.
- From the command line (Maven/Gradle): Useful for building and running without an IDE.
- As an executable JAR file: The standard way to deploy Spring Boot applications.
Understanding these methods will allow you to develop, test, and deploy your Spring Boot applications effectively. In the next section, we'll dive into Spring Boot's auto-configuration features.