Command 'java' not found on WSL, even though 'java' command works in Powershell

Solution 1:

There are a few... unique things about WSL that matter here. First, if you type the name of a .exe that is in the Windows side, but from WSL, it will work. For example, open a Bash prompt in WSL, type notepad.exe, and press enter. Notepad will open.

Before you uninstalled OpenJDK 8, you had openjdk-8-jre installed in WSL and Java 17 installed in Windows. When you called java from Windows, it was smart enough to add the .exe, and run the Windows copy of Java. But, when you switched to WSL, when you typed java, it ran the Linux version. But if you typed (into WSL) java.exe, it would have launched the Windows version of Java from WSL for the same reason that Notepad worked.

If I wanted to do Java development from WSL, I would uninstall the Windows version of Java completely, install my desired version of Java in WSL (sudo apt update && sudo apt install openjdk-17-jre), and just do development from in WSL via the WSL Java compiler.