How to Set Up and Run Java Spring Boot Applications

Getting Started with Java and Spring Boot
Welcome to the Getting Started page! In this guide, you'll learn how to set up Java, Spring Boot, and create your first Spring Boot project using start.spring.io.
1. Setting Up Java
To build and run Spring Boot applications, you'll need to install Java Development Kit (JDK). Follow the steps below to install Java.
Install Java JDK
-
Download Java JDK: Head to the Oracle JDK Downloads page and download the latest JDK version for your operating system.
-
Install JDK:
- Windows: Run the
.exe
file and follow the installation instructions. - macOS: Use the
.dmg
installer or install via Homebrew:brew install openjdk
- Linux: Install using your package manager. For example, on Ubuntu:
sudo apt install openjdk-17-jdk
- Windows: Run the
-
Verify Installation: Open your terminal or command prompt and run:
java -version
2. Setting Up Spring Boot
Once Java is installed, the next step is setting up Spring Boot.
Using start.spring.io
To create your first Spring Boot project, we’ll use start.spring.io, which provides a simple and quick way to initialize a Spring Boot application.
-
Go to start.spring.io:
Visit start.spring.io. -
Project Setup:
- Project Type: Choose Maven or Gradle (Maven is recommended for beginners).
- Language: Select Java.
- Spring Boot Version: Use the latest stable version.
- Group: Enter your desired group name (e.g.,
com.example
). - Artifact: Enter a name for your project (e.g.,
my-first-springboot-app
). - Dependencies: Add the following dependencies:
- Spring Web (for REST APIs)
- Spring Boot DevTools (for development purposes)
- Spring Data JPA (for database interaction)
- H2 Database (for an in-memory database)
-
Generate the Project:
Click Generate to download the project. This will download a.zip
file. -
Unzip the Project:
Unzip the file you downloaded. You will now have a project folder that contains all the files needed to run your Spring Boot application.
3. Importing the Project into Your IDE
Now that you’ve generated and unzipped the Spring Boot project, the next step is to import it into your favorite Integrated Development Environment (IDE).
Using IntelliJ IDEA or Eclipse
-
Open your IDE:
Launch IntelliJ IDEA or Eclipse. -
Import the Project:
- In IntelliJ IDEA, go to
File > Open
and select the unpacked project folder. - In Eclipse, go to
File > Import > Existing Maven Project
, then browse to the unpacked project folder and click Finish.
- In IntelliJ IDEA, go to
-
Wait for Dependencies to Download:
Your IDE will automatically download and set up the dependencies specified in thepom.xml
file (for Maven). -
Verify Project Structure:
Once the project is loaded, ensure that your source folder (src/main/java
) and resources folder (src/main/resources
) are correctly configured.
4. Running the Application
Now that your project is imported, let’s run the Spring Boot application.
-
Locate the Main Class:
Navigate to thesrc/main/java
folder and find the class annotated with@SpringBootApplication
(usually namedApplication.java
or similar). -
Run the Application:
- In IntelliJ IDEA, right-click on the main class and select Run.
- In Eclipse, right-click the project, go to
Run As > Spring Boot App
.
-
Check the Console:
Once the application starts, you should see Spring Boot running on the default port 8080. Your console will display logs indicating that the application is running successfully. -
Test the Application:
Open a browser and navigate tohttp://localhost:8080
to see the default response.
Congratulations! You've successfully set up and run your first Spring Boot application.