System Design
Design With Sid
java

How to Set Up and Run Java Spring Boot Applications

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

  1. Download Java JDK: Head to the Oracle JDK Downloads page and download the latest JDK version for your operating system.

  2. 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
  3. 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.

  1. Go to start.spring.io:
    Visit start.spring.io.

  2. 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)
  3. Generate the Project:
    Click Generate to download the project. This will download a .zip file.

  4. 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

  1. Open your IDE:
    Launch IntelliJ IDEA or Eclipse.

  2. 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.
  3. Wait for Dependencies to Download:
    Your IDE will automatically download and set up the dependencies specified in the pom.xml file (for Maven).

  4. 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.

  1. Locate the Main Class:
    Navigate to the src/main/java folder and find the class annotated with @SpringBootApplication (usually named Application.java or similar).

  2. 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.
  3. 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.

  4. Test the Application:
    Open a browser and navigate to http://localhost:8080 to see the default response.

Congratulations! You've successfully set up and run your first Spring Boot application.