Marcin Jakuszko

Development environment for Java on Windows (WSL2 + SDKMAN + IntelliJ)

Recently, I’ve moved away from using Mac (OS X) as my main development daily driver to a Windows laptop. I’ve needed to set up a convenient development environment that:

Installing WSL2 with Ubuntu 20.04

As a first step, you need to install and enable Windows Subsystem for Linux version 2. This operation requires local administrator rights on your machine (as you may expected 😁). I strictly followed instructions described here under manual installation steps.

Installing SDKMAN

There comes a time to install Java. I am using SDKMAN since a few years now, so I’ve decided to go with it this time as well. I went through installation manual (https://sdkman.io/install). This gets me into an issue. First I needed to install obvious dependency - curl.

$ sudo apt-get update $ sudo apt-get install curl After that, as manual is stating I’ve invoked command $ curl -s "https://get.sdkman.io" | bash but nothing happened. It turned out that WSL2 Ubuntu is missing more dependencies. I’ve needed to install zip/unzip. $ sudo apt-get install zip unzip Now the installation process went fine. I’ve decided to install OpenJDK 11 with Gradle 7.

Installing Windows Terminal (optional)

All of above commands I was typing in traditional windows command prompt which gives me a pain. As it is recommended on Microsoft WSL2 installation manual I’ve installed Windows Terminal app. This led mi into a couple of tweaks which were essential for me.

Switch default path after opening new terminal for WSL — Ubuntu-20.04.
We need to open settings, go to JSON settings file and add proper entry under ubuntu profile, in my case it looks like (replace $USER with your user name):

{
  "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
  "hidden": false,
  "name": "Ubuntu-20.04",
  "source": "Windows.Terminal.Wsl",
  "startingDirectory": "\\\\wsl$\\Ubuntu-20.04\\home\\$USER\\"
}

What is worth to mention — you need to be cautious about your distro name in the path — it needs to be exact full name.

Switch default prompt to Ubuntu one.
As previously we need to alter JSON settings file, switching the deault profile. In order to do this, we need to switch GUID in a default profile section, picking the one that is listeg in Ubuntu profile.

"defaultProfile": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}"

Working with IntelliJ IDEA 2021.1

JetBrains built in WSL2 support in the latest (back then) version of IntelliJ. In theory everything should work seamlessly after I’ve load a project from WSL2 network share
wsl$\Ubuntu-20.04 — JDK should be discovered along with gradle. It didn’t happen. Instead I’ve seen on my build console:

Connection refused: no further information

It turned out that I’ve needed to open my firewall for IntelliJ, to be able to connect to WSL2. Fortunately there is an instruction on a JetBrains site (here). After that I was able to successfully build, run and debug my Java application running inside WSL2 using IntelliJ installed on Windows.

#java #tutorial #windows