73

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.

16

9 Answers 9

55

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 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
3
  • 4
    Same problem, and fixing the "CurrentJDK" cured it for me.
    – MikeB
    Nov 7, 2014 at 21:25
  • I am root but still get rm: CurrentJDK: Operation not permitted
    – raoulsson
    Feb 6, 2016 at 17:14
  • Had to turn off rootless: https://www.quora.com/How-do-I-turn-off-the-rootless-in-OS-X-El-Capitan-10-11
    – raoulsson
    Feb 6, 2016 at 18:33
45

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
3
  • 1
    I couldn't find the place where JAVA_HOME is set (incorrectly) - I tried the ones you list, plus many more (/etc/environment, /etc/profile, /etc/profile.d/*, ...). In the end I decided to put unset JAVA_HOME to my ~/.bashrc - that's currently probably the only way to make it work automatically with Ubuntu's update-alternatives in most cases (since most programs try to figure out the correct JAVA_HOME on their own if it is not set and one of the first things they try is to look where the java executable points to). Feb 19, 2015 at 12:42
  • If you can unset it, you should also be able to simply override the existing value.
    – Kat
    Feb 19, 2015 at 20:30
  • If I set JAVA_HOME in ~/.bashrc it will be set for the whole session and won't change if I run update-alternatives and stay in the same session. I want most of the apps, and especially maven at this moment, to choose the JVM version set by update-alternatives. Unfortunately, Ubuntu doesn't consider this problem important enough: bugs.launchpad.net/ubuntu/+source/java-common/+bug/45348 Feb 19, 2015 at 23:02
27

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.

1
  • Perfect finding...was struggling since long time with this...Above setting helped.. Jul 18, 2019 at 9:47
27

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
2
  • 2
    in my case it was sudo update-alternatives --config javac Are you sure "alternatives" is not a typo?
    – PbxMan
    Mar 13, 2017 at 9:10
  • update-alternatives is relevant to Debian and Ubuntu, whereas alternatives is relevant to CentOS/RedHat.
    – Paulie-C
    Sep 13, 2019 at 10:07
14

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

3
  • 1
    Somehow, I didn't notice this answer until too late, but it would have revealed the fact that ~/.mavenrc was overriding JAVA_HOME, so +1.
    – Kat
    Jul 11, 2014 at 21:07
  • I think specifically settings JAVA_HOME (e.g. in .bash_login using export JAVA_HOME=/usr/libexex/java_home -v 1.8) is the best option
    – scravy
    Jan 17, 2015 at 13:45
  • I agree with @scravy - export JAVA_HOME=$(/usr/libexec/java_home -v1.8)
    – joensson
    Feb 6, 2015 at 13:59
4

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>
2

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.

0

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.

5
  • Exactly what am I looking for? There's a JRE folder there (as well as bin, db, lib, and include). As far as I can tell, it seems to be the same as the other JREs. I downloaded this from jdk8.java.net/download.html. And it works with Eclipse (I tested a lambda).
    – Kat
    Jul 11, 2014 at 20:38
  • 1
    The latest release of Java 8 is 1.8.0_05. The jdk1.8.0_20-ea indicates it's the 20th EARLY ACCESS release of Java 8, in other words, the 20th sneak peek of what will eventually become 1.8.0, which is now in it's fifth "patch release" or 1.8.0_05. Basically, once you can get a non-ea build, go for it.
    – Edwin Buck
    Jul 11, 2014 at 20:44
  • I just tried 1.8.0_05 (actually, I tried that first) and Maven still does not recognize it. All I did was set JAVA_HOME (which other posts seem to imply is all that's needed). As an aside, is 1.8.0_05 newer than 1.8.0_20-ea?
    – Kat
    Jul 11, 2014 at 20:50
  • 1
    Yes, 1.8.0_20-ea is earlier than 1.8.0_05. From oldest to newest, it's more ordered 1.8.0_20-ea, 1.8.0, 1.8.0_05. Check a couple of things. which java and then a ls -l of the returned value (as it can redirect you independently of the JAVA_HOME and also check your PATH environment variable, as it might be overriding which java is found before you get to the items you are checking.
    – Edwin Buck
    Jul 11, 2014 at 20:58
  • Thanks. I should be using 1.8.0_05 then. I was confused over which version was the latest version.
    – Kat
    Jul 11, 2014 at 21:09
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.