How to set JAVA_HOME from the ubuntu shell on WSL2?

I installed WSL2 on my Windows 10 Home. I want to use Maven (which I verified is installed correctly on Windows).

In an ubuntu shell from Windows Terminal, I set JAVA_HOME in my .profile but it would not get set. I ran these commands to try out the correct way to set JAVA_HOME but they do not work.

[email protected]:/mnt/c/Users/user$ mvn
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

[email protected]:/mnt/c/Users/user$ JAVA_HOME=/mnt/c/Program Files/Java/jdk-11.0.10/bin
[email protected]:/mnt/c/Users/user$ cd $JAVA_HOME
-bash: cd: too many arguments

[email protected]:/mnt/c/Users/user$ cd /mnt/c/Program Files/Java/jdk-11.0.10/bin
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ mvn --version
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

[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ JAVA_HOME=/mnt/c/'Program Files'/Java/jdk-11.0.10/bin
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments

[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ JAVA_HOME=/mnt/c/'Program Files'/Java/jdk-11.0.10
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments

[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd /mnt/c/Program Files/Java/jdk-11.0.10/bin
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ pwd
/mnt/c/Program Files/Java/jdk-11.0.10/bin

[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ echo $JAVA_HOME
/mnt/c/Program Files/Java/jdk-11.0.10
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$


[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ export JAVA_HOME="/mnt/c/Program Files/Java/jdk-11.0.10"
[email protected]:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments

[email protected]:~$ export JAVA_HOME="/mnt/c/Program Files/Java/jdk-11.0.10"
[email protected]:~$ cd $JAVA_HOME
-bash: cd: too many arguments

UPDATE:

I followed user Aran’s (unfortunately deleted) answer below – I edited /etc/environment and added

export JAVA_HOME="/mnt/c/Program Files/Java/jdk-11.0.10"

then in .bashrc I added

source /etc/environment

and thought it worked because when I did an env in the ubuntu shell, I saw JAVA_HOME

I installed mvn and it still gives the same error:

$ mvn 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 

I think it is the space in “Program Files”. I do have JDK 11 installed. Verified by navigating to JAVA_HOME/bin and I can see javac.exe.

So the issue is not fixed

Note: This is not a duplicate of this question. I do not have access to how Maven is accessing JAVA_HOME. The question is not about cd $JAVA_HOME. It is about using mvn Maven. If Maven is using $JAVA_HOME internally, there is no way I can see how to make it use "$JAVA_HOME"

Answer

Set the JAVA_HOME variable from Windows, via the gui and use Windows Powershell instead of ubuntu shell.

Java home

Leave a Reply

Your email address will not be published. Required fields are marked *