I have a project that contains the .pom
file. Based on the instruction, I should run the mvn install
command first. When I run the command I get the following error:
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
Caused by: org.codehaus.plexus.compiler.CompilerException: invalid flag: --release
Caused by: java.lang.IllegalArgumentException: invalid flag: --release
But, when I run the command with sudo
(sudo mvn install
), I get the following error:
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
However, the result of mvn -version
command is as follows:
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 1.8.0_265, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-42-generic", arch: "amd64", family: "unix"
On the other hand, I've added the followings to ~/.bashrc
file:
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=/usr/share/maven/bin/mvn:$PATH
export PATH=$JAVA_HOME/bin:$PATH
The result of echo $JAVA_HOME
is:
/usr/lib/jvm/java-8-openjdk-amd64
and the result of java -version
is :
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
Besides, I've considered many related posts to my issue, such as post1, post2, post3, and post4 (which are about maven in windows, opposite to my Ubuntu OS) or post5 (that I have not the same problem, as I am not on a VM or any problem with mvn -version
. However, mvn version command points to JRE, but all environment variables have been set correctly to point to the JDK). But, none of them resolve it.
When you run a command using sudo
, the environment variables from your current (non-privileged) shell are NOT passed through to the environment in which the command is run.
Try this:
$ export FOO=BAR
$ sudo export
and you won't see FOO in the list of variables.
So, when you run sudo mvn ...
the JAVA_HOME
setting is not being passed to Maven, and it says so.
If you want to pass all of the environment variables, use sudo -E
or similar. This is explained in the manual entry for sudo
.