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 in wsl2?
java - Installing Oracle JDK on Windows subsystem for Linux - Stack Overflow
Has anyone succeeded running Java 8 on WSL?
At this time, I do not believe that it is possible to install any version of Java onto WSL mainly because of the current extremely limited functionality and stability with it being a feature that has not gone public.
More on reddit.comUnable to install java-8-openjdk
Videos
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...
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
Regardless of openjdk or oracle, can't get it install through apt, configuration fails with ca-certificates-java.
Manually downloading oracle jdk and running root@localhost:/tmp# /usr/lib/jdk8/bin/java -version just hangs until I kill it.
At this time, I do not believe that it is possible to install any version of Java onto WSL mainly because of the current extremely limited functionality and stability with it being a feature that has not gone public.
I think they fixed installing with apt, but it can hang when running code. Then try with -Xint.
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
I'm used to working on linux but is forced to use windows so I would like to set up WSL2 but haven't fully managed to do so. My tools available to use are intellij, java, mvn, angular, docker(docker desktop)
-
I've installed everything I can in WSL2(mvn, java, angular and so on)
-
My team uses Docker Desktop on macOs so I've installed that with wsl2 and ubuntu integration enabled. I can compile create images and containers in wsl2 using terminal commands. The first issue I have is that in linux i limited resources for docker using a slice. Docker desktop limits will affect my ubuntu wsl2 which I don't want. But if I don't limit it then the RAM usage will end up at 93% and I will notice lag in windows and slow compilation. Is it better to setup docker in wsl2 and skip docker desktop or are there any other alternatives?
-
Intellij is installed locally on windows. Repos are placed in wsl2 path. In intellij i will open these repo and point the project sdk to java8 in wsl. However, I can't run unit tests with intellij. There will be an error "Failed to find compatible JDK". What is the best practice here? Do i need to download java for windows and point to that instead?
There are a few... unique things about WSL that matter here. First, if you type the name of a .exe that is in the Windows side, but from WSL, it will work. For example, open a Bash prompt in WSL, type notepad.exe, and press enter. Notepad will open.
Before you uninstalled OpenJDK 8, you had openjdk-8-jre installed in WSL and Java 17 installed in Windows. When you called java from Windows, it was smart enough to add the .exe, and run the Windows copy of Java. But, when you switched to WSL, when you typed java, it ran the Linux version. But if you typed (into WSL) java.exe, it would have launched the Windows version of Java from WSL for the same reason that Notepad worked.
If I wanted to do Java development from WSL, I would uninstall the Windows version of Java completely, install my desired version of Java in WSL (sudo apt update && sudo apt install openjdk-17-jre), and just do development from in WSL via the WSL Java compiler.
You can simply use java.exe instead of java in WSL. I provided the commands so that you can
- create a new java file (using
nano <class_name>.java) - compile it (
javac.exe <class_name>.java) - run and see the output (
java.exe <class_name>)
I ran the following commands:
cd /mnt/c/users/adity/desktop
nano Basic.java
javac.exe Basic.java
java.exe Basic
The code in the Basic.java file:
public class Basic {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Here's the screenshot of the terminal:
