Diagnosis first:
echo "Path (since javac should be on it): $PATH"
echo "JAVA_HOME (since it overrides path): $JAVA_HOME"
# consider also
which java
# and follow the links to find which Java you are using
java -version
mvn -v
If javac
or java
aren't on path, consider checking if they are - at all - installed.
Perhaps adding symlinks to /usr/bin
will be enough.
If path doesn't have binaries from Java bin
directory, javac cannot execute (command not found).
Most Java apps expect $JAVA_HOME and will check for it. Enabling it allows them to pick which Java they should use. Also, this overrides path, so if you have more than one Java installed...
export JAVA_HOME=/usr/lib/jvm/...
if this works in terminal...- set in in
~/.mavenrc
- set it in your shell profile:
~/.bash_profile
or~/.zprofile
for ZSH - set it in
/etc/environment
or other system-wide place
Drawback: other apps (except for 2.) will be affected as well.
To make things clear, this is extracted from mvn
script:
if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
So, now you know the order and what overrides what.
Set compiler properly, so it uses Java you want it to use. This can be used to compile for lower Java than you use!
<properties>
<maven.compiler.target>1.9</maven.compiler.target>
<maven.compiler.source>1.9</maven.compiler.source>
</properties>
check parent POM - if applies.
You can set this in plugin as well.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
See Examples here: https://maven.apache.org/plugins/maven-compiler-plugin/
Use jenv
or update-java-alternatives
or for MacOS CurrentJDK
https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html