'Maven works only if run as sudo

I've been recently trying to set-up a java-env on an Ubuntu machine. My current problem is that maven won't pick up the JAVA_HOME as executable, and consequently fails:

$ JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
$ mvn -v
/usr/lib/jvm/java-17-oracle/bin/java
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

If I run mvn as sudo, the Java is found normally:

$ sudo mvn -v
/usr/bin/java
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.1, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.13.0-30-generic", arch: "amd64", family: "unix"

I would prefer not to use the privileges, as it can cause down-stream problems. I was wondering whether this is a known issue that can be solved?

Thanks



Solution 1:[1]

Solved this by having a custom installation of maven, linked to Jenv. If you are doing this, be careful to:

  1. have matching Java versions (double check this)
  2. No JDK collisions
  3. permissions ok
  4. MVN recognizes JDK/bin/java as executable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sdgaw erzswer