Run the following commands as a user with sudo privileges or root to update the packages index and install the OpenJDK 11 JDK package:

Copy$ sudo apt update
$ sudo apt install openjdk-11-jdk

Once the installation is complete, you can verify it by checking the Java version:

Copy$ java -version

The output should look something like this:

Copyopenjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Set JAVA_HOME Environment Variable: OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Once you found the path of your preferred Java installation, open the /etc/environment file:

Copy$ sudo nano /etc/environment

Assuming you want to set JAVA_HOME to point to OpenJDK 11, add the following line, at the end of the file:

CopyJAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

For changes to take effect on your current shell you can either log out and log in or run the following source command:

Copy$ source /etc/environment

Verify that the JAVA_HOME environment variable was correctly set:

Copy$ echo $JAVA_HOME

You should see the path to the Java installation:

Copy/usr/lib/jvm/java-11-openjdk-amd64

for reference you can follow this link below How to Install Java on Ubuntu 20.04

Answer from Karam on Stack Overflow
🌐
GitHub
gist.github.com › Sawaba › 22f3c3d4ec9a12dd972c2ecc71e4eca9
Java JDK 11 install script for Windows Subsystem for Linux (WSL) · GitHub
Anyone still using this: you can simply download it through the browser on your windows 10 machine, and then on your WSL navigate to /mnt/ which is where you drives are mounted, then you can copy the .tar.gz file from your mounted drive to your ...
Top answer
1 of 7
78

Run the following commands as a user with sudo privileges or root to update the packages index and install the OpenJDK 11 JDK package:

Copy$ sudo apt update
$ sudo apt install openjdk-11-jdk

Once the installation is complete, you can verify it by checking the Java version:

Copy$ java -version

The output should look something like this:

Copyopenjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Set JAVA_HOME Environment Variable: OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Once you found the path of your preferred Java installation, open the /etc/environment file:

Copy$ sudo nano /etc/environment

Assuming you want to set JAVA_HOME to point to OpenJDK 11, add the following line, at the end of the file:

CopyJAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

For changes to take effect on your current shell you can either log out and log in or run the following source command:

Copy$ source /etc/environment

Verify that the JAVA_HOME environment variable was correctly set:

Copy$ echo $JAVA_HOME

You should see the path to the Java installation:

Copy/usr/lib/jvm/java-11-openjdk-amd64

for reference you can follow this link below How to Install Java on Ubuntu 20.04

2 of 7
36

We can use the same JDK which is installed in Windows inside the wsl2. For that, we should add this to /etc/environment

CopyJAVA_HOME=/mnt/c/Program Files/Java/jdk-11.0.8/bin/

by adding this bin folder we may run regular commands but with .exe suffix eg: javac.exe hello.java java.exe hello.java

if you don't like that way then add alias like below:

Copyalias java='java.exe'
alias javac='javac.exe'

I think we can use any of the windows programs like this :)

Discussions

java - Installing Oracle JDK on Windows subsystem for Linux - Stack Overflow
Note WSL requires .exe since it is a Windows binary. When using the Linux distro of the OpenJDK, no extension is required.. ... also don't forget to add to your .bashrc: alias java='java.exe' alias javac='javac.exe' 2025-01-04T17:48:27.11Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
Java in wsl2?
Can I code in java in Vscode? yep If yes what extension should i use? the Java extension :) you are probably wanting to connect your VSCode to WSL. then all coding happens in WSL environment automatically. check this and to install Java to WSL in the first place, check this More on reddit.com
🌐 r/bashonubuntuonwindows
8
3
August 26, 2024
Help with setting up vscode for java on wsl
i tried to setup java on vscode, but i ended up using intellij. More on reddit.com
🌐 r/vscode
16
18
May 3, 2020
Installation error on windows 11 using WSL 2
I've been trying to install this but keep getting this error log: ERROR: JAVA_HOME is set to an invalid directory: /mnt/c/Program Files/Java/jdk-1.8 Please set the JAVA_HOME variable in your en... More on github.com
🌐 github.com
6
June 24, 2024
🌐
.NET Thailand
dotnetthailand.com › programming-cookbook › wsl-powershell-useful-scripts › install-java
Install Java on WSL2 | .NET Thailand
sudo apt install openjdk-8-jdk · Edit .bashrc file to export JAVA_HOME variable: Terminal window · sudo vi ~/.bashrc · export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 · Open a new terminal session and verify if we have set Java home correclty · Terminal window ·
🌐
Medium
medium.com › @pierre.viara › install-java-on-windows-10-linux-subsystem-875f1f286ee8
Install Java on Windows 10 Linux subsystem | by Pierre Viara | Medium
October 10, 2020 - sudo apt install openjdk-11-jdk · Press enter or click to view image in full size · Once it is done, I invite you to type the following command line and check that Java JDK version is 11.X.X : java --version · Press enter or click to view ...
🌐
DEV Community
dev.to › mumu › windows-subsystem-for-linux-java-setup-2b2b
Windows Subsystem for Linux Java Setup - DEV Community
May 24, 2021 - Now we need a couple more tools to get the system Java ready. sudo apt update will pull down updated mirrors for downloading applications from. sudo apt install openjdk-11-jdk maven will install the Java Development Kit (necessary for any Java ...
🌐
Kontext
kontext.tech › home › blogs › java programming › install open jdk on wsl
Install Open JDK on WSL - Kontext Labs
February 2, 2021 - Wait until the installation is completed. ... ~$ java -version openjdk version "1.8.0_275" OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-1~deb9u1-b01) OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode) Configure JAVA_HOMEenvironment variable is optional but I highly recommend it especially if you want to install Hadoop, Spark, Hive, HBase or any other Java based frameworks.
🌐
w3tutorials
w3tutorials.net › blog › what-is-the-proper-way-of-using-jdk-on-wsl2-on-windows-10
What is the Proper Way to Use JDK on WSL2 (Ubuntu 20.04) on Windows 10? Install on Linux or Use Windows JDK for Java Programming? — w3tutorials.net
VS Code: Install the WSL Extension. It auto-detects Linux JDK and runs the IDE in WSL context. IntelliJ: Use the WSL Remote Development plugin to connect to WSL2 and use the Linux JDK. # Update Linux JDK (apt) sudo apt upgrade openjdk-11-jdk # Update WSL2 (Windows PowerShell) wsl --update · Fix: Ensure PATH includes the JDK bin directory. Recheck ~/.bashrc for typos in JAVA_HOME.
Find elsewhere
Top answer
1 of 14
93

I wanted to clarify that as of 9 December 2016, you most certainly can install Java 8 on Ubuntu Bash for Windows 10 and that @Karl Horton is correct.

You will need to install unzip sudo apt-get install unzip

Copy this script somewhere in your bash for windows session and make it executable (chmod +x filename). If you do not use a command line based editor such as vim then you will have windows line endings to deal with. you can use dos2unix or your preferred way of dealing with that. I just paste it into a file using vim.

 #!/bin/bash

set -ex

# UPDATE THESE URLs
export JDK_URL=http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
export UNLIMITED_STRENGTH_URL=http://download.oracle.com/otn-pub/java/jce/8/jce_policy-8.zip

# Download Oracle Java 8 accepting the license
wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
${JDK_URL}
# Extract the archive
tar -xzvf jdk-*.tar.gz
# clean up the tar
rm -fr jdk-*.tar.gz
# mk the jvm dir
sudo mkdir -p /usr/lib/jvm
# move the server jre
sudo mv jdk1.8* /usr/lib/jvm/oracle_jdk8

# install unlimited strength policy
wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
${UNLIMITED_STRENGTH_URL}
unzip jce_policy-8.zip
mv UnlimitedJCEPolicyJDK8/local_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/
mv UnlimitedJCEPolicyJDK8/US_export_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle_jdk8/jre/bin/java 2000
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle_jdk8/bin/javac 2000

sudo echo "export J2SDKDIR=/usr/lib/jvm/oracle_jdk8
export J2REDIR=/usr/lib/jvm/oracle_jdk8/jre
export PATH=$PATH:/usr/lib/jvm/oracle_jdk8/bin:/usr/lib/jvm/oracle_jdk8/db/bin:/usr/lib/jvm/oracle_jdk8/jre/bin
export JAVA_HOME=/usr/lib/jvm/oracle_jdk8
export DERBY_HOME=/usr/lib/jvm/oracle_jdk8/db" | sudo tee -a /etc/profile.d/oraclejdk.sh

And now I can do the following

fieldju@DESKTOP-LTL6MIC:~$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)                                                

The links and versions in the above script are likely to be out of date by the time you read this, so just head over to http://www.oracle.com/technetwork/java/javase/downloads/index.html accept the license so that their js lets you copy the new URLs and you should be good to go.

2 of 14
69

It seems in 2017 august the solution is simpler as suggested by @noah-david.

I was able to install Oracle JDK 8 from the “WebUpd8” team repository.

Instructions. To add the repository:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

To install:

sudo apt-get install oracle-java8-installer 
sudo apt install oracle-java8-set-default

After install

costin@amanta-win:/mnt/c/work$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

costin@amanta-win:/mnt/c/work$ which java
/usr/bin/java

costin@amanta-win:/mnt/c/work$ uname -a
Linux amanta-win 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 
  x86_64 x86_64 x86_64 GNU/Linux
🌐
Reddit
reddit.com › r/bashonubuntuonwindows › java in wsl2?
r/bashonubuntuonwindows on Reddit: Java in wsl2?
August 26, 2024 -

Hey, I'm really new to linux developpement but I will be using this os for my studies so I'm triying to learn a few things. I installed wsl2 with ubuntu 22.04 instead of a dual boot because it seemed better. It works very well, especially with vscode. I even use X410 to have a perfect linux environnement.

However, I will be studiying java and I don't know how to proceed. I guess that my files will be stored in the wsl2 file system. Please explain me a few things :

Can I code in java in Vscode? If yes what extension should i use?

If no what should I intsall to code in java and how do I install it so i can run smoothly it with wsl2 files?

Thanks for explaining, really new to all of this so sorry if this is a stupid question...

🌐
Microsoft Learn
learn.microsoft.com › en-us › java › openjdk › install
Install the Microsoft Build of OpenJDK | Microsoft Learn
brew install --cask microsoft-openjdk@11 # OR brew install --cask microsoft-openjdk@17 # OR brew install --cask microsoft-openjdk@21 # OR brew install --cask microsoft-openjdk@25 · To uninstall the Microsoft Build of OpenJDK installed through ...
🌐
Medium
medium.com › @pravinpreneur › how-to-modernize-java-development-environment-using-vs-code-and-wsl2-to-improve-productivity-1c9681390170
How to Modernize Java Development Environment using VS Code and WSL2 to Improve Productivity | by PravinPreneur | Medium
December 10, 2023 - Use below step by step guide to setup Java Development Environment using VS Code and WSL2: Installing Windows Subsystem for Linux (WSL) 2 on Windows allows you to run Linux distributions directly on your Windows machine without the need for a virtual machine. This is a great way to use Linux tools and applications without having to switch to a different operating system. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 ...
🌐
Medium
medium.com › @vpriyanshu671 › java-development-kit-jdk-22-installation-guide-for-wsl-455f34676b45
Java Development Kit (JDK) 22 Installation Guide for WSL | by Priyanshu Verma | Medium
August 5, 2024 - This guide provides step-by-step instructions for removing older Java versions and installing JDK 22 on a Windows Subsystem for Linux (WSL) environment. Remove existing OpenJDK packages: sudo apt remove openjdk-11-jdk openjdk-11-jre openjdk-21-jdk openjdk-21-jre sudo apt autoremove ·
🌐
CSE CGI Server
cgi.cse.unsw.edu.au › ~cs2511 › 26T1 › setup › linux-wsl-setup
🐧 Linux/WSL (Ubuntu) | COMP2511 (26T1)
April 18, 2026 - Selection Command ----------------------------------------------- *+ 1 java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64/bin/java) 2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.12.0.7-2.fc40.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: It is important you install exactly Gradle 9.2.
🌐
Reddit
reddit.com › r/vscode › help with setting up vscode for java on wsl
r/vscode on Reddit: Help with setting up vscode for java on wsl
May 3, 2020 -

Hello, I'm trying to set up my development environment and I happen across this guide:

https://code.visualstudio.com/docs/languages/java

I followed the instructions and it worked on regular Microsoft environment and I can run everything. However, when I tried doing development from wsl ubuntu 18.04 things became a bit messy. I'm unable to run or debug code from vscode, I can't call in other custom packages I've made and vscode keeps on telling me that jdk isn't installed eventhough I know its on both my windows and ubuntu wsl.

Somethings I've done:

1.) sudo apt install java 11 on wsl. I've double checked and it exist I can run simple java programs from the terminal but it can't call other classes or packages I've made.

2.) Tried pointing java.home on vscode to the java in wsl. this is the user/bin/java filepath but it doesn't work and was not expecting it too since it didn't look similar to how I set the path on regular windows to jdk11. For reference I placed this path by doing "which java" in terminal.

Any help would be deeply appreciated.

here's how the error comes up. During the regular windows version it doesn't show since I followed the guide provided for installing jdk11.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-do-I-install-Java-on-Ubuntu
How to install Java 21 on Ubuntu
Install the JDK with the following command: sudo apt install default-jre Setting up default-jdk-headless (2:1.21) ... Setting up openjdk-21-jdk:amd64 (21.0.5+11-1ubuntu~24.04.1) ...
🌐
GitHub
github.com › minerllabs › minerl › issues › 776
Installation error on windows 11 using WSL 2 · Issue #776 · minerllabs/minerl
June 24, 2024 - I think my java is set up wrong but when I do java -version on my WSL and command line I get 1.8.x, I set up the java_home variable with these commands: setx JAVA_HOME "C:\Program Files\Java\jdk-1.8" setx WSLENV "JAVA_HOME/p"
Author   minerllabs
🌐
Medium
medium.com › @remekgdansk › setting-up-development-environment-in-windows-subsystem-for-linux-wsl-in-windows-11-cae1dec1be64
Setting up development environment in Windows Subsystem for Linux (WSL) in Windows 11 | by RemekGdansk | Medium
January 2, 2023 - Go to ~/demo folder and run mvn clean install. Run mvn spring-boot:run to start the application. Now you can open localhost:8080 in the browser or curl localhost:8080 from either Windows or WSL.
🌐
CSE CGI Server
cgi.cse.unsw.edu.au › ~cs2511 › 25T2 › setup › linux-wsl-setup
🐧 Linux/WSL (Ubuntu) | COMP2511 (25T2)
April 18, 2026 - Selection Command ----------------------------------------------- *+ 1 java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64/bin/java) 2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.12.0.7-2.fc40.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: It is important you install exactly Gradle 8.8.
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › commandLine › wsl.html
Installation Instructions for WSL
Importantly, Java must be installed separately for WSL (even if you have already installed Java for Windows) To do this, first make sure your Ubuntu's internal list of available software packages and their versions from online repositories is up to date by typing the following in a Ubuntu (in Terminal) prompt