WSL Bash isn't finding java in PATH

Viewed 3881

I have an export command in my .bashrc to add the path of the java.exe file to PATH. Right now, running echo $PATH gives me this at the end

/mnt/c/Program Files/Java/jdk-14.0.2/bin

This is exactly where the java.exe and javac.exe files are stored, but when I run something like java -version I'm getting the Command 'java' not found error. What am I doing wrong here?

3 Answers

You have added the Windows version of the Java binaries to the Path. You use WSL to run Linux binaries. (WSL1 is a compatibility layer, WSL2 uses the Linux kernel).

You have two options:

  1. Install Java in your WSL environment. For example on Ubuntu with following commands:

    sudo apt update
    sudo apt install openjdk-14-jdk
    

    After installing Java it will be available in the search path.

  2. You could also run the Windows version by calling java.exe (note the .exe) on the name. This way WSL would call the Windows version. (https://docs.microsoft.com/en-us/windows/wsl/interop#run-windows-tools-from-linux)

If you want to use the windows version of java for some reason you where almost there. Try this please :)

java.exe -version

Install java in wsl:

sudo apt-get -y install openjdk-14-jdk 

check the version.

java –version 

check the path to config JAVA_HOME.

sudo update-alternatives --config java 

eg

/usr/lib/jvm/java-14-openjdk-amd64/bin/java. 

edit environment file to add the path:

   sudo nano /etc/environment 

add declare and add the JAVA_HOME:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$JAVA_HOME/bin" 

JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64 

save and check the path.

source /etc/environment 

echo $JAVA_HOME 

Output

/usr/lib/jvm/java-14-openjdk-amd64  
Related