Windows WSL Ubuntu 22 doesn't recognise the upgraded Java version I installed on Windows

335 Views Asked by At

I'm a newbie and am confused over the setup for one of my cs courses. I originally have Java 8 in both Windows and Ubuntu, but then I realised that I need to use jshell for testing in Ubuntu as well, which is only available for Java 9 and higher.

Hence I downloaded the newest Java 19 on my windows but Ubuntu doesn't recognise it and still use Java 8. How can I go around to update the Java in Ubuntu as well without having to redownload it again?

Below is on my Ubuntu terminal:

hejin@LAPTOP-8I6A5M2K:~/cs2030s$ java.exe -version
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)
hejin@LAPTOP-8I6A5M2K:~/cs2030s$ javac.exe -version
javac 1.8.0_351
hejin@LAPTOP-8I6A5M2K:~/cs2030s$ jshell.exe
jshell.exe: command not found
hejin@LAPTOP-8I6A5M2K:~/cs2030s$ jshell
Command 'jshell' not found, but can be installed with:
sudo apt install openjdk-11-jdk-headless  # version 11.0.17+8-1ubuntu2~22.04, or
sudo apt install openjdk-17-jdk-headless  # version 17.0.5+8-2ubuntu1~22.04
sudo apt install openjdk-18-jdk-headless  # version 18.0.2+9-2~22.04
sudo apt install openjdk-19-jdk-headless  # version 19.0.1+10-1ubuntu1~22.04

(to be honest I don't know why it seems others can do fine with just java Hello.txt and javac Hello.txt but I have to add .exe at the back.

And below is my command prompt terminal:

C:\Users\User>java -version
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

C:\Users\User>javac -version
javac 19.0.1

C:\Users\User>jshell -version
jshell 19.0.1

C:\Users\User>jshell
|  Welcome to JShell -- Version 19.0.1
|  For an introduction type: /help intro

jshell> 1 + 1
$1 ==> 2

jshell>

I am confused over what some answers I searched online talked about changing the environment variable path as well... When I checked the advanced system settings the jdk19 path seems already there. screenshot of my system settings environment variables

1

There are 1 best solutions below

0
On

Here are a few things to check with your path dealings Windows -> WSL Ubuntu. On Windows you can confirm where java is found by:

echo %Path%
=> should print list of colon separated folders on your path example:

C:\java\jdk-20\bin;etc

which java.exe
=> should print name of java.exe found inside %Path% example:
C:\java\jdk-20\bin\java.exe

When you run wsl.exe to load WSL Linux distribution the Windows Path is translated to Linux semi-colon separated PATH without drive letters:

C:\java\jdk-20\bin;etc => /mnt/c/linux/jdk-20/bin:etc

On WSL Linux check where it looks up PATH:

echo $PATH
=> should print list of colon separated folders on your path:
/mnt/c/linux/jdk-20/bin:etc
=> its likely you have 2 JDK in Linux PATH

which java.exe
=> finds "java.exe" of the first JDK in the Linux PATH

Have a look at any WSL Ubuntu bash profiles which may also override PATH in WSL to see what causes the 2nd JDK in path - typically ~/.bashrc or ~/.profile and fix so the later JDK19 is first or only JDK one resolved.

Note that fixing WSL Linux to access Windows JDK introduces a new issue if you actually need Linux JDK, but you could always use "java" vs "java.exe" from WSL if you need to work in both JDK platforms from Linux.