Score:1

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

ae flag

This topic probably is a common one, but when looking online I didn't see much that related to my case, since I'm not running a normal ubuntu, but rather WSL (Windows Subsystem for Linux).

So my problem is that I switched from java 8 to java 17 recently (I required java 8 for a course), but I noticed that on ubuntu terminals, java -version still showed openjdk-1.8.(something), rather than jdk-17 or something like that.

So I thought that uninstalling openjdk would do the trick, which I did with sudo apt-get autoremove openjdk-8-jre. However, now typing java in an ubuntu terminal doesn't work anymore at all, and shows: Command 'java' not found, but can be installed with: .... But I fear that installing it with one of these commands will install a new version of java, rather than using the one that's already installed.

I have set the JAVA_HOME variable to the java installation directory, which is mnt/c/Program Files/Java/jdk-17.0.1, but the space in Program Files does seem to be a problem.

How can I fix this? I usually prefer using a bash terminal than Powershell or cmd because I know the commands better, so not being able to use java or javac in bash is an issue for me. Thanks!

Score:4
cn flag

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.

Thibaut B. avatar
ae flag
That's interesting! I tried `java.exe` and `javac.exe` in the bash terminal and it does work properly! Is it "bad" to do this (i.e. should I uninstall windows java and do sudo apt install), or is it fine but just less intuitive/easy?
cocomac avatar
cn flag
I would uninstall Windows java and just `sudo apt install openjdk-17-jdk`. 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.
Score:0
ye flag

You can simply use java.exe instead of java in WSL.[I have added an image link so as to how you can

  1. create a new java file (using nano <class_name>.java)
  2. compile it (javac.exe <class_name>.java)
  3. run and see the output (java.exe <class_name>)

The code in the file(Basic.java) is :

    public class Basic{
        public static void main(String[] args) {
            System.out.println("Hello World");
        }
    }

]
image link

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.