Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 713
Mushfiqur Rahman Abir Avatar asked Sep 13 '20 03:09

Mushfiqur Rahman Abir


People also ask

How do I run a Java program in WSL?

Press ctrl + shift + p , type "install extensions", press enter, and search for "java red hat", and install "Java Language Support" by Red Hat. Done! You're all set up to develop a Java project in Ubuntu in WSL!

Where is JDK installed on WSL?

As printed out, JDK is installed in folder /usr/lib/jvm/java-8-openjdk-amd64.

How do I enable JDK in Windows 10?

In a browser, go to the Java SE Development Kit 10 Downloads page and click Accept License Agreement. Under the Download menu, click the Download link that corresponds to the .exe for your version of Windows. Download the file jdk-10.

Can I use Windows Java in WSL?

If you are going to develop in Linux, it's better to use the Linux version of Java. Technically, calling the Windows compiler from WSL will work in theory, but it might create unexpected problems like line endings, so I would just uninstall Windows Java and just install the JDK in Ubuntu.


1 Answers

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

like image 140
Karam Avatar answered Oct 05 '22 23:10

Karam