[VSCode] Try Java
Tags: java
Published: 2023-10-30 17:31:14

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

* [Amazon Corretto](https://aws.amazon.com/corretto/?filtered-posts.sort-by=item.additionalFields.createdDate&filtered-posts.sort-order=desc)

## Creating a Java project without any build tools
First, I installed "Extension Pack for Java" into my VSCode.

* [Extension Pack for Java - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
* [Extension Pack for Java](vscode:extension/vscjava.vscode-java-pack)

* [Java in Visual Studio Code](https://code.visualstudio.com/docs/languages/java)

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
```

### Changing build settings
I can change building settings for Java from settings.json.

#### settings.json
```json
{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}
```

### 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
```json
...
    ],
    "java.autobuild.enabled": false
}
```

### 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
```json
...
    "java.autobuild.enabled": true,
    "files.encoding": "shiftjis",
}
```

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

* [Spring Boot Extension Pack - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=vmware.vscode-boot-dev-pack)
* [Gradle for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle)
* [Maven and Gradle support for Java in Visual Studio Code](https://code.visualstudio.com/docs/java/java-build)

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

#### build.gradle
```groovy
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()
}
```

#### SpringbootsampleApplication.java
```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);
	}
}
```

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

#### build.gradle
```groovy
...
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
...
```

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

#### HomeController.java
```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);
    }
}
```

* [Spring Quickstart Guide](https://spring.io/quickstart)