vS-code-start-project

In this document I will explain how to set up the Spring Boot environment in VS Code. You will find the instructions to install and configure,

  • JDK 19
  • Gradle 7.6
  • Visual Studio Code (VS Code)

I will also create and run a basic spring boot application.

Introduction

Spring boot is an open source, production-ready java development framework suitable for stand-alone applications, which run on Java Virtual Machine (JVM). Spring boot is quite popular framework to develop the Restful microservices, Model-View-Controller (MVC) applications, etc.

Visual Studio Code is a free, lightweight Integrated Development Environment (IDE) by Microsoft. It is a great addition to Java code editing tools, and a good alternate to IntelliJ, eclipse etc.

Gradle is a build automation tool for multiple language development. It makes the development tasks much easier, such as compile, build, test, package, etc.

Let’s start setting up the environment..

Operating System

The instructions written in this document is valid for Windows 10 or above.

Java 19:

We start with installing the latest available (at the time of writing) JDK 19. Go to JDK 19 download page, and download Windows x64 msi Installer and install.

Gradle

I will install the latest available Gradle version, i.e. 7.6, because earlier versions (like 7.5) are not compatible with JDK19. We will download the archive file from this link,

Select “v7.6” and then Download: binary only. This will download the Gradle-7.6-bin.zip file.

Create a new directory C:\Gradle (or any preferred location). Uncompress the Gradle-7.6-bin.zip file in C:\Gradle.

Environment Variables

You can use any command line tool to set environment variables. Here, we'll use Windows PowerShell. First, let's check the existing value of JAVA_HOME to see if it is already set. Open Windows PowerShell and type:

$Env:JAVA_HOME

Note: $ is the part of the command.

If it returns an empty string, it infers JAVA_HOME is not set. Let’s set it now,

$Env:JAVA_HOME = "C:\Program Files\Java\jdk-19"

Now set GRADLE_HOME value,

$Env:GRADLE_HOME  = "C:\Gradle\gradle-7.6"

Add JAVA_HOME and GRADLE_HOME to PATH variable, so that the shell recognise java and gradle commands,

$Env:PATH += $Env:JAVA_HOME + ";\bin"
$Env:PATH += $Env:GRADLE_HOME + ";\bin"

Verify environment variable values,

echo $Env:JAVA_HOME
echo $Env:GRADLE_HOME
echo $Env:PATH

Set environment variable using windows system properties,

We can also set the envirnment variable in Windows System Properties. For that, in Windows Start menu search for “Edit the system environment variable”,

Add JAVA_HOME and Gradle_Home system environment variables,

Add JAVA_HOME\bin and GRADLE_HOME\bin to the system PATH variable.

Spring Boot environment

Install VS Code

Install Visual studio Code (VS Code) from following link,

https://code.visualstudio.com/download

VS Code Extensions:

For better productivity, we may install following extensions in VS Code,

  • Extension pack Java
  • Spring boot Extension pack
  • Gradle for java

You can install extension by clicking "Extensions" on left menu and search required ones,

Configure JDK 19 in VS Code

we will In order to create springo boot application

In VS Code go to File/Preferences/Settings, and then select Extensions from the left menu. Expand the Extension to find a menu for Java. Scroll down to click on Edit in Settings.json.

OR

You can use command Pallet, Shift + Ctrl + P

or just press F1

Search for “Preferences: Open User Settings (JSON)” and add JDK and gradle home path and spring initializr service URL in json body,

"Java.jdt.ls.java.home" = “C:\\Program Files\\Java\\jdk-19”
"java.import.gradle.home": "C:\\Gradle\\gradle-7.6"
"spring.initializr.serviceUrl": "https://start.spring.io/"

Note: On Windows backslashes must be escaped, so C:\ must be written as C:\\

Spring initializr service URL is required to launch spring boot initializr. By default its value is empty, if we do not set it, and create a spring boot project using spring initializer we will get,

Failed to create a project. Failed to fetch metadata.

Create Sprint Boot Application

Now we all set to launch spring initializer to create a new spring boot application,

Press Shift + Ctrl + P or F1 to open the command palette

Following are my project settings for this example,

Specify Spring version: 3.0.1

Specify project language: Java

Input Group Id for your project: com.example.mygroup

Input artifact Id: myfirstspringbootapp

Specify packaging type: Jar

Specify Java version: 19

Search for dependencies: let’s select Lombok, Spring Web (for web, Restful application using MVC). you can select other dependencies as well.

Press Enter

Select the destination directory to save the project. Spring Initializr will generate a templated project for spring boot application. This is how it looks after we finalize initialization.

We can see src and test directories with their respective java classes. We also see the build.gradle file which consists of all the dependencies, plugins, and different tasks.

You can run this app now. Open the src.java.com.example.mygroup.myfirstspringbootapp/MyfirstspringbootappApplication.java file and run,

We will see Spring boot application started.

This is just a start to something great you can achieve with spring boot, Java and Gradle. Hope this will help you to take the first step toward that direction.

To deepen your understanding of Spring Boot Microservices and Docker containers, I highly recommend reading this article: Quick Start to Spring Boot Microservices and Docker. By following that blog, you will be creating a spring boot application, and add a controller to process HTTEP GET requests. You will also learn about how to package your spring boot application using docker container.


© 2024 Solution Toolkit . All rights reserved.