DEV Community

Masui Masanori
Masui Masanori

Posted on

[VSCode] Try Java

Intro

I will try writing Java with VSCode.
In this time, I won't use build tools like Maven, Gradle, etc.
And I will use Amazon Corretto.

Creating a Java project without any build tools

First, I installed "Extension Pack for Java" into my VSCode.

After installing, I create a Java project by Ctrl + Shift + P -> "Java:Create Java Project...".
After creating, the project has four folders and four files.

JavaSample
  L.vscode
    Lsettings.json
  Lbin
    LApp.class
  Llib
  Lsrc
    LApp.java
  LREADME.md
Enter fullscreen mode Exit fullscreen mode

Changing build settings

I can change building settings for Java from settings.json.

settings.json

{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}
Enter fullscreen mode Exit fullscreen mode

Stopping auto compile

By default, when I change Java files, the compiler will recompile automatically.
But in some cases, I want to stop it.

To do this, I add "java.autobuild.enabled".

settings.json

...
    ],
    "java.autobuild.enabled": false
}
Enter fullscreen mode Exit fullscreen mode

Changing default file encodings

By default, VSCode open files with UTF-8 encoding.
But in some cases, I want to change it for specific projects.

Adding "files.encoding" changes the default file encodings for the project.

settings.json

...
    "java.autobuild.enabled": true,
    "files.encoding": "shiftjis",
}
Enter fullscreen mode Exit fullscreen mode

Creating a Springboot project

To create Springboot projects, I installed "Spring Boot Extension Pack" and "Gradle for Java".

I selected Gradle as the build tool and I didn't add any dependencies.

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.5'
    id 'io.spring.dependency-management' version '1.1.3'
}

group = 'jp.masanori'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}
Enter fullscreen mode Exit fullscreen mode

SpringbootsampleApplication.java

package jp.masanori.springbootsample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootsampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootsampleApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Adding a controller class

To create Web API, I added "org.springframework.boot:spring-boot-starter-web" into build.gradle.

build.gradle

...
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
...
Enter fullscreen mode Exit fullscreen mode

Because Springboot automatically maps URLs through annotations, so I just need to add a controller class.

HomeController.java

package jp.masanori.springbootsample;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
      return String.format("Hello %s!", name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)