Java 8 development on Linux/WSL with Visual Studio Code on Windows 10

Linux on Windows via WSL 2, has become a great development environment when targeting cloud containers and functions.  Windows has a shot at becoming the favorite desktop for users building Linux applications.

Visual Studio Code (VSCode) is a great IDE. VSCode can run in a split GUI/Server fashion with the GUI in Windows and all the full SDLC executing on Linux.
  • Development happens inside the Linux, WSL2 leveraging VS Code Server
  • The GUI happens as a real part of the Windows Desktop and connects to Remote VSCode servers, in this case in WSL Linux

Ooh A Video

Java Development

VSCode's Java integration is built on top of Java 11.  This means you probably will end up with Java 11 JDK installed in WSL2 Linux and in Windows.  By default, applications will be Compiled with Java 11.  

You can target application compilation against versions other than the one used by the IDE. This is done by installing the needed Java versions on the Linux side and adding the Java installation locations to the list of targeting options in the settings.  In my case, I'm targeting Java 8.

 

Managing VSCode Java Extensions


VSCode extensions can be installed in the local, GUI, VSCode or they can be installed in the remove VSCode servers. In this case, we are doing development in a remote server in Linux so we need to install extensions in both places.
  • The Java Extensions Pack must be installed in the remote VSCode server.
  • Several Remote enablement extensions must be installed locally.

Setting the IDE Java Version and the Default Target

The Java Runtime configuration is used by the Java Extension.  It will also be used as part of the build and execution process if no other configuration is done.
  1.  Ctrl-Shift-P
  2. Java: Configure Java Runtime

Standard Configuration

The Java Development Kit looks in 4 different places to find the current JDK in the following order. This is the version of Java used by the extension and as part of building and testing Java applications.

Making all Installed JDK Versions Visible to Project

We want to make all installed versions visible to the IDE for compilation, testing and, execution. We will make configuration changes in the appropriate settings.json file.  

Find and set the Preferences

  1. Ctrl-Shift-P
  2. Enter Preferences
  3. Select any of the Preferences: Open options

Three Different Preferences with Different Scopes

You can see here the three different scopes 
  1. User scoped on the windows host
  2. User scoped on the Linux host
  3. Project Workspace scoped

Telling the Java / Eclipse Extensions where Java is

We need to manually tell the VS Code server what Java versions are available.  No standard Java environment variables support this so we will configure it in settings.json.

  1. Click on the  Remote [WSL: Ubuntu] tab
  2. Click on the Edit in settings.json
  3. Make the following change in  ~.vscode-server/data/Machine/settings.json
{
    "java.configuration.runtimes": [
        {
          "name""JavaSE-1.8",
          "path""/usr/lib/jvm/java-8-openjdk-amd64",
        },
        {
          "name""JavaSE-11",
          "path""/usr/lib/jvm/java-11-openjdk-amd64",
        },
    ],
}

Your project should now see Java 8 and build/run with it if configured to do so

Disclaimer

This all assumes you want to compile and test and possibly run using Java 8.  This is useful when the target platform, Function, Compute doesn't support the same version of Java as the VSCode Java Extension.

Comments

Popular posts from this blog

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Almost PaaS Document Parsing with Tika and AWS Elastic Beanstalk