Can't get Maven to recognize Java 1.8

JavaMaven

Java Problem Overview


I can't seem to be able to get Maven to use Java 1.8. Using 1.8 as the target turns up the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
(default-compile) on project csaro: Fatal error compiling: invalid target
release: 1.8 -> [Help 1]

The cause of the error is obvious enough: Maven isn't using the right version of Java:

$ mvn -version
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T07:51:42-06:00)
Maven home: /usr/local/Cellar/maven/3.2.2/libexec
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"

But the installed version of Java should be 1.8:

$ java -version
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b22)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b21, mixed mode)

And JAVA_HOME is set:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

I also tried the command here (which creates mavenrc). I've tried restarting the computer several times, and have verified that the env var is correctly set (it's set in .bash_profile).

Maven was installed with Homebrew.

Java 1.8 is working fine in Eclipse (which is using m2e). I just can't get Maven to work on the command line.

Java Solutions


Solution 1 - Java

For me the issue was that when I installed from jdk-8u25-macosx-x64.dmg the installation did not update the /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK directory link to the new 1.8 JDK. This may not have been the issue of the original question but it was the solution for me when I encountered the same error message.

I don't know why the installer doesn't link it and to make matters more confusing, the path is different depending on whether you installed it from Oracle or Apple. See https://stackoverflow.com/questions/6141180/mac-os-x-10-6-7-java-path-current-jdk-confusing

I did the following to fix my environment

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/<installed_jdk_version>.jdk/Contents CurrentJDK

Solution 2 - Java

It turns out that I had a forgotten ~/.mavenrc file that had set the value of JAVA_HOME.

For future readers, check the following locations for places that may override JAVA_HOME (in ascending order of precedence):

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.bashrc
  • ~/.profile
  • /etc/mavenrc
  • ~/.mavenrc

Solution 3 - Java

The solution which worked for me:

In Eclipse, I had a wrong run configuration in my Maven build. Go to

> Run Configurations -> JRE [Tab]

and set the runtime to Java 8.

Solution 4 - Java

I ran into the same problem on my CentOS 6.5 system.

java -version returned a Java 8 version, but javac -version returned a Java 7 version.

I had to run the following commands to get all kinds of Java-related symlinks point to my JDK 8 installation:

sudo alternatives --config java
sudo alternatives --config javac
sudo alternatives --config jre_openjdk
sudo alternatives --config java_sdk_openjdk

Solution 5 - Java

Your JAVA_HOME is being overridden from somewhere else. Try echoing in mvn right before java invocation.

Solution 6 - Java

Note that you can also get the "invalid target release" error with Java 11

> [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler- > plugin:3.8.0:compile (default-compile) on project projects-migration: > Fatal error compiling: error: invalid target release: 1.11 -> [Help 1]

if you set 1.11 instead of just 11 in the POM.

Incorrect:

<java.version>1.11</java.version>

Correct:

<java.version>11</java.version>

Solution 7 - Java

I had same problem, needed 2 steps to make correct:

  1. Ensure JAVA_HOME is set pointing correctly to JDK;
  2. JDK and JRE in eclipse should point the same version.

For me 1 step was right, 2nd step had problem. In eclipse » Project's properties » Build Path » Installed JRE » point to JDK.

Solution 8 - Java

You are using an early access release of 1.8. Now that is has been released, you should install the released version.

Check you maven installation to see if you have a configuration associated with it that specifies a particular JDK. Occasionally there are wrapper scripts which reset JAVA_HOME prior to maven launch, or the maven executable is launched in a wrapper that refers to a config file that could bind you to a particular JVM.

Solution 9 - Java

If you are using Maven within a continuous integration server be sure to check it's settings as it may override the JDK used by Maven. For Jenkins, I had to add the new 1.8 JDK in the general settings and configure my project to use it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKatView Question on Stackoverflow
Solution 1 - JavaKirbyView Answer on Stackoverflow
Solution 2 - JavaKatView Answer on Stackoverflow
Solution 3 - JavaBevorView Answer on Stackoverflow
Solution 4 - JavaAbdullView Answer on Stackoverflow
Solution 5 - JavajmjView Answer on Stackoverflow
Solution 6 - JavaAndrew SpencerView Answer on Stackoverflow
Solution 7 - Javasandeep pandeyView Answer on Stackoverflow
Solution 8 - JavaEdwin BuckView Answer on Stackoverflow
Solution 9 - JavaHenno VermeulenView Answer on Stackoverflow