Skip to content

Instantly share code, notes, and snippets.

@CTXz
Last active May 24, 2020 22:56
Show Gist options
  • Save CTXz/b01a5da52e3101d811bd6e1e4d86742b to your computer and use it in GitHub Desktop.
Save CTXz/b01a5da52e3101d811bd6e1e4d86742b to your computer and use it in GitHub Desktop.

Getting Java to work with VS Code on Ubuntu has been a rather frustrating journey. Knowing how prone I am to forgetting things, and considering how likely it is that I'll have to configure VScode for java some time again, I figured it'd be good if I wrote down the few successful milestones to get VScode to do what I actually want it to fucking do.

Installing the Java development kit and Java runtime environment

Unless a newer or older version is explicitly required, I recommend sticking to ubuntu's default jdk and jre packages:

$ sudo apt install default-jdk default-jre

Should you require multiple java versions, use

$ sudo update-alternatives --config java

to alter between java version

Setting the JAVA_HOME variable

Next we must set the JAVA_HOME variable. In the year 2020 you'd maybe expect Ubuntu to already do this for you, but for whatever reason that's not the case (I'm sure some hardcore java or ubuntu users are furiously shouting why this is a good thing, but I'm a computer engineer, I would never fuck with java if it wasn't for the fact that one of my university courses use it as the preffered language, so I don't fucking care, it makes my life harder).

To set the JAVA_HOME variable to the path of the default jdk installation path, add the following lines to /etc/profile

JAVA_HOME="/usr/lib/jvm/default-java/"
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Installing JUnit

Getting JUnit working was a frustrating fucking mess. This is solely because there isn't a single fucking stack overflow post out there, at least as far as I'm concerned, that provides you with a step by step guide written for three year olds. Now, knowing an average developers increadibly short patience and attention span, any problem that reaches this point becomes practically insolvable.

As it turns out, the junit package for ubuntu's package list sucks dick and won't do with VS code. Grab the latest junit standalone library from here instead and move it to /usr/share/java.

Setting up VS code

First, install the Java Extension Pack extension.

Next we're going to define the java home and java libraries path in the settings.json file. To do so, press CTRL + SHIFT + P and enter Open Settings and select Preferences: Open Settings (JSON). In the settings.json, add the following lines:

    "java.home" : "/usr/lib/jvm/default-java/",
    
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "/usr/share/java/*.jar",
    ]

If you're adding these lines to the end of the json file, don't forget to add a comma at the end of the previous content!

Last but not least, clean the Java language server workspace by pressing CTRL + SHIFT + P, entering Clean the Java language server workspace and selecting Java: Clean the Java language server workspace.

Now you should be good to go to work on Java projects and run JUnit tests in Vscode!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment