3

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)
markvgti
  • 4,321
  • 7
  • 40
  • 62
  • Just a vague idea, but do you by any chance have `true` in your pom? If so, try making that `false` and see what happens – g00se Jul 24 '21 at 17:02
  • There is no pom, no project. I was just trying to see that Maven would run correctly inside WSL2. – markvgti Jul 24 '21 at 17:04
  • That's that theory out of the window then ;) Are you sure if's the *same* Maven you're running in each OS? – g00se Jul 24 '21 at 17:07
  • You probably can't run maven under WSL (in bash) with a Windows JDK. You could try a Linux JDK instead. – Jorn Vernee Jul 24 '21 at 17:08
  • >Maven home: C:\Maven\bin\..< seems a little strange. What happens when you do this? `export MAVEN_HOME=/mnt/c/Maven;mvn -v` – g00se Jul 24 '21 at 17:15
  • @g00se Still the same error. – markvgti Jul 24 '21 at 17:16
  • @JornVernee if we're supposed to run the Windows JDK in WSL2, shouldn't we be running the Windows Maven as well? – markvgti Jul 24 '21 at 17:17
  • If you want to force running maven with the Windows JDK, you can't invoke `mvn` from `bash`. But you can run from `cmd` with e.g.: `$ cmd.exe /C "mvn --version"` – Jorn Vernee Jul 24 '21 at 17:19
  • Maybe try exporting both variables before invoking `mvn` then? – g00se Jul 24 '21 at 17:27
  • 2
    I am trying to use Quarkus, which involves using Docker, which involves running code from inside the WSL2 filesystem. Shouldn't I be running Maven from inside WSL2 as well? – markvgti Jul 24 '21 at 17:30
  • @g00se I tried setting 1st `MAVEN_HOME` & then `M2_HOME`, but neither worked. The values were visible in `wsl`, but Maven still wouldn't work. – markvgti Jul 24 '21 at 17:45
  • Em… Are you sure you installed a JDK and not a JRE? What does `javac -version` tells? – Auktis Jul 25 '21 at 08:55
  • @Auktis Yes, it's definitely JDK. `java -version` says `javac 11.0.10`. – markvgti Jul 25 '21 at 18:12
  • @markvgti Maybe the best is to install Maven and Java on Ubuntu. That should work like a charm. – Auktis Jul 25 '21 at 18:48

2 Answers2

1

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.

markvgti
  • 4,321
  • 7
  • 40
  • 62
0

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.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70