- Microsoft, Java Extension Pack, https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack
- Temurin, https://adoptium.net/
- Amazon, Corretto 17, https://docs.aws.amazon.com/corretto/latest/corretto-17-ug/downloads-list.html
- Azule Systems, Zule OpenJDK, https://www.azul.com/downloads/
- OpenJDK, https://jdk.java.net/
- Red Hat, OpenJDK, https://developers.redhat.com/products/openjdk/download/
The path to the Java Development Kit is searched in the following order:
- the
java.home
setting in VS Code settings (workspace then user settings) - the
JDK_HOME
environment variable - the
JAVA_HOME
environment variable - on the current system path
- build.gradle
repositories {
mavenCentral()
}
apply plugin: 'java'
sourceCompatibility = 17
targetCompatibility = 17
compileTestJava {
options.compilerArgs += '-parameters'
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.9.2')
}
- .vscode/tasks.json
{
"version": "2.0.0",
"echoCommand": true,
"command": "./gradlew",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"tasks": [
{
"label": "compileJava",
"type": "shell"
},
{
"label": "test",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell"
},
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch)",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"preLaunchTask": "compileJava",
"mainClass": "com.vscode.demo.Main",
"args": ""
}
]
}
- src/main/java/com/vscode/demo/Main.java
package com.vscode.demo;
import java.util.Objects;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println("Hello VS Code!");
Stream.of("React", "Angular", "Vue")
.filter(x -> Objects.equals(x, "React"))
.forEach(System.out::println);
}
}
- src/main/java/com/vscode/demo/MainTest.java
package com.vscode.demo;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Tag("main")
class MainTest {
@Test
@DisplayName("VS Code JUnit 5 test")
void testMain() {
// arrange
final List<String> list = Arrays.asList("React", "Angular", "Vue");
// act
final String actual = list.stream()
.filter(x -> Objects.equals(x, "React"))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
// assert
assertEquals("React", actual, () -> "Main Succeed");
}
}
- Open
Main.java
- Press
F5
- Open Command Pallete
cmd+shift+p
(macOS) orctrl+shift+p
(Windonws/Linux) Tasks Run Test Task
test
- Visual Studio Code, Java in VS Code, https://code.visualstudio.com/docs/languages/java
- GitHub, Language Support for Java(TM) by Red Hat, https://github.com/redhat-developer/vscode-java
- Visual Studio Code, Integrate with External Tools via Tasks, Variable substitution, https://code.visualstudio.com/Docs/editor/tasks#_variable-substitution
- Gradle, Chapter 47. The Java Plugin, https://docs.gradle.org/current/userguide/java_plugin.html
- GitHub, JUnit 5 Samples, https://github.com/junit-team/junit5-samples
- Gradle, Gradle 4.6 Release Notes - JUnit 5 support, https://docs.gradle.org/4.6/release-notes.html#junit-5-support