Maven on WSL2 can't see JAVA_HOME

Viewed 2325

I have JDK 11 and Maven 3.8.1 installed on my Windows 10 machine. Took care that install paths don't have spaces.

Made the value of JAVA_HOME visible inside the Ubuntu shell following the advice here. Then I restarted the Ubuntu shell.

Here are the outputs from inside an Ubuntu shell on Windows Terminal:

user@computer:/path$ java -version
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)

user@computer:/path$ env | grep JAVA
JAVA_HOME=/mnt/c/AdoptOpenJDK/jdk-11-hotspot

user@computer:/path$ mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

So although the value of JAVA_HOME is visible inside the Ubuntu shell, somehow mvn can't see it. How do I fix this?

Maven run from cmd gives this output:

C:\path>mvn -v
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: C:\Maven\bin\..
Java version: 11.0.10, vendor: AdoptOpenJDK, runtime: C:\AdoptOpenJDK\jdk-11-hotspot
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Versions:

  • Windows 10 version 2004 (OS Build 19041.1110)
  • Java 11.0.10
  • Maven 3.8.1
  • WSL2
  • Ubuntu 20.04.2 LTS (via WSL2)
2 Answers

I gave up and simply installed JDK 11 & Maven 3.8.1 inside WSL2 using SDKMAN.

Not an ideal solution, but didn't feel like spending more time on this issue.

I don't disagree with your solution to use the Linux versions of Java and Maven inside WSL, but I do think I see why your example in the original question was failing.

The problem appears to be that you are using the Windows versions of Java/Maven, BUT your JAVA_HOME from inside WSL is using the "Linux form" of the path (/mnt/c/AdoptOpenJDK/jdk-11-hotspot).

Maven is complaining, because since it is running under Windows, it doesn't understand that path. It needs to see C:\AdoptOpenJDK\jdk-11-hotspot.

I believe this is because the answer you linked to said to use setx WSLENV "JAVA_HOME/p". The /p is forcing the translation from the Windows path to the WSL/Linux mapping, which you don't want in this case.

Also, be sure to restart the Terminal from which you launch WSL so that it can pick up the new environment variables.

Related