Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

Jawsh picture Jawsh · Nov 18, 2018 · Viewed 15.4k times · Source

I'm trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash. JAVA_HOME Env. Var.

<code>enter image description here</code>

What's at the end of my bashrc:

export JAVA_HOME="/mnt/d/Program Files/Java/jdk-11.0.1"
export PATH="/mnt/d/Program Files/Java/jdk-11.0.1/bin:$PATH"

CMD INPUT/OUTPUT:

C:\Users\jaall>javac --version
javac 11.0.1

BASH INPUT/OUTPUT:

myubuntu_name@DESKTOP-LUK3BII:~$ javac --version

Command 'javac' not found, but can be installed with:

sudo apt install default-jdk
sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless

I've been stuck on this for awhile and can't figure it out or find a working solution online. Thanks!

Answer

Mbuotidem Isaac picture Mbuotidem Isaac · Dec 1, 2018

As Biswapriyo suggested, you should use WSLENV.

  1. Open PowerShell. Then set JAVA_HOME to the path to your java installation.

  2. In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"

You should see a message that says "SUCCESS: Specified value was saved.

  1. Then run setx WSLENV "JAVA_HOME/p"

You should see the success message again.

  1. Type 'env' into your WSL bash prompt.

You should see JAVA_HOME correctly set at this point.

Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the '\bin' folder.