39

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?

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • Installed things do not _hog CPU/RAM_ unless you use those - they only take up space. – Sнаđошƒаӽ Mar 14 '21 at 18:31
  • 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 right – Mushfiqur Rahman Abir Mar 15 '21 at 15:03
  • 1
    For what it's worth, [here](https://itnext.io/using-wsl-2-to-develop-java-application-on-windows-8aac1123c59b) 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 configuration – Aserre Dec 03 '21 at 07:42

5 Answers5

37

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

Karam
  • 411
  • 4
  • 10
15

We can use that Windows JDK inside the wsl2. 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 append with .exe format 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 :)

KR32
  • 381
  • 5
  • 10
8

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.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    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. – Mushfiqur Rahman Abir Sep 13 '20 at 05:18
  • That is what I am saying. (Or more precisely, that is what other people are saying.) – Stephen C Sep 13 '20 at 05:19
  • https://www.jetbrains.com/help/idea/how-to-use-wsl-development-environment-in-product.html#open-a-project-in-wsl – Amund Mar 17 '22 at 14:16
2

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
JustABit
  • 425
  • 4
  • 11
2

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.