2

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!

2 Answers 2

5

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.

2
  • 1
    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?
    – Thibaut B.
    Commented Jan 10, 2022 at 17:01
  • 2
    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.
    – cocomac
    Commented Jan 10, 2022 at 17:03
1

You can simply use java.exe instead of java in WSL. I provided the commands so that 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>)

I ran the following commands:

cd /mnt/c/users/adity/desktop 
nano Basic.java
javac.exe Basic.java
java.exe Basic

The code in the Basic.java file:

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

Here's the screenshot of the terminal: Screenshot of the Ubuntu terminal with the commands to create Basic.java, compile and run it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .