What is the proper way of using JDK on WSL2 on Windows 10?
Asked Answered
W

6

51

I have installed Ubuntu 20.4 LTS on WSL. My windows 10 already have the JDK installed. Do I need to install JDK on ubuntu on WSL or can I use the Windows 10 JDK in the Ubuntu? How you do Java programming on WSL? Which is the proper way?

I was just wondering if I need to install all the development tools and binaries again on Linux won't it take a lot of space & hog a lot of CPU/Ram resources?

Warily answered 13/9, 2020 at 3:14 Comment(3)
Installed things do not hog CPU/RAM unless you use those - they only take up space.Cornflower
Yeah, but running both JDK on wsl & JDK on windows won't hog CPU/RAM? But yeah I got what you are saying & you are rightWarily
For what it's worth, here is a medium post comparing perfomances of a native JDK vs a WSL2 jdk, both in command line and inside IntelliJ. As always, your milleage may vary depending on your configurationIncident
F
9

There is not a "proper" (as in supported or recommended by JDK providers) way to install or use Java on WSL. I could not find any official recommendations.

However, it is possible to either install and use Oracle JDK for Windows installation from WSL, or install OpenJDK Java into your WSL world from the Ubuntu package manager.

I was just wondering if I need to install all the development tools and binaries again on Linux won't it take a lot of space & hog a lot of CPU/Ram resources ?

See above. But note that you are only going to "hog CPU/RAM" if you are running both kinds of JVM at the same time.

References:

(There are many more articles on this topic if the above don't address your concerns.)

Fahlband answered 13/9, 2020 at 5:8 Comment(3)
Yeah you are right there seems no documentation about this on either Windows or Oracle about this. So, you are saying that I can use both the JDK in Windows and in WSL without any worry ? By the way, thanks a lot for your answer.Warily
That is what I am saying. (Or more precisely, that is what other people are saying.)Fahlband
jetbrains.com/help/idea/…Cataphyll
E
53

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

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

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

$ java -version

The output should look something like this:

openjdk 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:

$ 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:

JAVA_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:

$ source /etc/environment

Verify that the JAVA_HOME environment variable was correctly set:

$ echo $JAVA_HOME

You should see the path to the Java installation:

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

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

Enkindle answered 26/11, 2020 at 0:47 Comment(1)
Thanks for the answer. But i was not actually asking for that, specially the Linux installation process.Warily
H
25

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

JAVA_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:

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

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

Holbrooke answered 12/6, 2021 at 4:44 Comment(3)
This is really the best way to do it.Engraving
+1 for suggesting aliases. In my case WSLENV was working fine, but which java returned nothing... With alias or using java.exe everything works as expected.Scull
And in order to use maven?Garek
F
9

There is not a "proper" (as in supported or recommended by JDK providers) way to install or use Java on WSL. I could not find any official recommendations.

However, it is possible to either install and use Oracle JDK for Windows installation from WSL, or install OpenJDK Java into your WSL world from the Ubuntu package manager.

I was just wondering if I need to install all the development tools and binaries again on Linux won't it take a lot of space & hog a lot of CPU/Ram resources ?

See above. But note that you are only going to "hog CPU/RAM" if you are running both kinds of JVM at the same time.

References:

(There are many more articles on this topic if the above don't address your concerns.)

Fahlband answered 13/9, 2020 at 5:8 Comment(3)
Yeah you are right there seems no documentation about this on either Windows or Oracle about this. So, you are saying that I can use both the JDK in Windows and in WSL without any worry ? By the way, thanks a lot for your answer.Warily
That is what I am saying. (Or more precisely, that is what other people are saying.)Fahlband
jetbrains.com/help/idea/…Cataphyll
R
4

Well I had faced same issue of installing jdk on wsl but I did'nt find any solution for directing putting the path of windows jdk path into wsl.

  1. So I followed an alterantive way, I had installed jdk on wsl/Ubuntu with cmd.

    sudo apt install default-jdk

or install any of jdk version.

  1. Before installing make sure to update ur packages/modules.

    sudo apt-get update

3.Get the path of the isntalled jdk

which java

The problem here is that the location it gives is actually a symbolic link. You’ll have to follow it a couple of times:

  1. Setting JAVA_HOME variable

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java

check the value of JAVA_HOME variable:

echo $JAVA_HOME
  1. This is not over yet. The JAVA_HOME variable you just declared is temporary. If you close the terminal or start a new session, it will be empty again.

    To set JAVA_HOME variable ‘permanently’, you should add it to the bashrc file in your home directory.

    Back up your bashrc file (in case you mess it, you can get it back):

cp ~/.bashrc ~/.bashrc.bak
 
 Next, use the echo command to append the export command you used at the beginning of this section. Change the command below to use the correct path as displayed by your system in.

echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" 
>> ~/.bashrc

Verify that it has been correctly added to the end of the file:

tail -3 ~/.bashrc

The above tail command will show the last 3 lines of the specified file.

Ranjiv answered 2/7, 2023 at 6:36 Comment(1)
It's a good approach I guess. But can you access JDK from Windows ?Warily
T
3

I installed Java via IntelliJ IDEA on Windows 11 and wanted to reuse the installation on WSL.

Create matching functions in ~/.bashrc to start the executables directly and export them so they can be used from subshells:

java() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-<version>/bin/java.exe "$@"
}
export -f java

javac() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-<version>/bin/javac.exe "$@"
}
export -f javac
Thermopylae answered 6/2, 2023 at 10:34 Comment(1)
Though it's an old thread but thanks this is a good option.Warily
W
3

Since the distribution of WSL Ubuntu you have may not support the latest version of the JDK, you can also download and install it.

Go to https://jdk.java.net/21/ for instance and select the "Linux / x64" (if appropriate) version to download.

Execute the following to specify this one as the Java version to run.

version=21
major_minor_patch=21.0.1

sudo tar xvf openjdk-${major_minor_patch}_linux-x64_bin.tar.gz -C /usr/lib/jvm
sudo ln -s /usr/lib/jvm/jdk-${major_minor_patch} /usr/lib/jvm/jdk-${version}

for p in java javac jar ; do sudo update-alternatives --install /usr/bin/$p $p /usr/lib/jvm/jdk-${version}/bin/$p 11111 ; done

Confirm selections:

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config jar
Willful answered 22/10, 2023 at 1:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.