Setup Java on WSL

Setup Java on WSL

Tags
Java
Setup
VSCode
Published
January 20, 2024
Author

Motivation

and Java in Visual Studio Code and see the reason I choose WSL instead of Windows.
 
As required by the course, I will install Java 8., which is a LTS version.
 
There are many distributed versions of JDK. For convenience, I will choose to install directly from apt

Steps

  1. (Optional) Ensures that you have the latest packages in WSL
    1. sudo apt update && sudo apt upgrade
  1. Verifying the availability of OpenJDK
    1. apt-cache search openjdk-8 | grep openjdk-8
      notion image
      💡
      Note jre is short for Java Runtime Environment, while jdk is for Java Development Kit.
  1. Install OpenJDK 8
    1. sudo apt install openjdk-8-jdk
      💡
      This command line will also install openjdk-8-jre automatically. So no bother to do it manually.
  1. Verify the installation
    1. java -version
      💡
      If you are installing Java 9+, java --version also works
      If something goes wrong, use which java to determine if you are using the java just installed, or you are actually using other version installed previously (as below):
      notion image
      Write the following lines into ~/.bashrc to prepend the Java of desired version to PATH.
      export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" export PATH="$JAVA_HOME/bin:$PATH"
      Don’t forget to run source ~/.bashrc to activate the added setting.
  1. (Optional) Configure runtime version and path
    1. In VScode, use Ctrl+Shift+P to open the Command Palette, search for Preference: Open Remote Settings (JSON) (WSL) and append the configuration below
      notion image
      { // other configurations ..., "java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "/usr/lib/jvm/java-8-openjdk-amd64", "default": true }, { "name": "JavaSE-11", "path": "/usr/lib/jvm/java-11-openjdk-amd64", }, { "name": "JavaSE-13", "path": "/usr/lib/jvm/java-13-openjdk-amd64", }, ] }
      💡
      You can set one of them to default by adding "default": true to the entry. To see which JDKs are used for your projects, you can trigger the command Java: Configure Java Runtime in Command Palette (Ctrl+Shift+P).
 
Done 👏
 
Use jenv to manage java version
 
Setup Java on windows
winget search jdk winget install Oracle.JDK.21
Download Maven here, e.g., apache-maven-3.9.6-bin.zip
unzip the zip file, add /bin to environment variables.

Reference