I needed a simple solution to compile a standalone Java file in VS Code.

TL;DR:

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "type": "shell",
            "command": "javac",
            "args": [
                "${file}"
            ]
        },
        {
            "label": "Run",
            "type": "shell",
            "dependsOn": "Compile",
            "command": "java",
            "options": {
                "cwd": "${fileDirname}"
            },
            "args": [
                //"${cwd}"
                "${fileBasenameNoExtension}"
            ],
            "presentation": {
                "reveal": "always",
                "panel": "new"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Type Ctrl+Shift+B, then choose whatever you like. Run obviously depends on Compile, so Run is really Compile and Run.

The file should be compiled into (a) .class file(s), then Java should run the main class file (which should exactly match the file name, or else it won’t run).

Enjoy!

Bonus

File > Preferences > User Snippets > Java

Inside the curly brackets, paste this:

"Print newline": {
	"prefix": "sout",
	"body": [
		"System.out.println(\"$0\")"
	],
	"description": "Prints a new line to System.out"
},
"For loop": {
	"prefix": "fori",
	"body": [
		"for (int i = ${1:init}; i < ${2:value}; i++) {$0",
		"}"
	],
	"description": "For loop with i variable"
}

I used this a lot with IntelliJ IDEA, and sout is obviously a lot better than typing sysout.

Shame IntelliJ IDEA is based on Java. There’s a problem with ibus-hangul that prevents me from writing Korean in IntelliJ IDEA. I hope it gets fixed soon, but until then VS Code seems like a very nice alternative.