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 OverflowRun 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
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 :)
java - Installing Oracle JDK on Windows subsystem for Linux - Stack Overflow
Java in wsl2?
Help with setting up vscode for java on wsl
Installation error on windows 11 using WSL 2
Videos
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.
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
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...
Here's how I just made Java "just work" for my WSL installation.
ln -s /mnt/c/Program\ Files\ \(x86\)/Java/jre1.8.0_151/bin/java.exe /bin/java
What this does is create a symbolic link in your /bin folder to your Windows Java binary. Whenever WSL tries to invoke Java now, it's redirected to the Windows version. You will probably need to adjust the path as Java versions change in the future.
This issue is because WSL does not support windows shortcuts in the Creator's update. /mnt/c/ProgramData/Oracle/Java/javapath/java.exe is actually a shortcut to the actual installation directory which is C:\Program Files\Java\jre1.8.0_31\bin in my case. Invoking java from the actual installation path seems to work as expected.
This issue is resolved in build 16193
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.