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.

Community
  • 1
  • 1
Kat
  • 4,645
  • 4
  • 29
  • 81
  • 3
    _jdk1.8.0_20_ hasn't released yet, and i don't see ea in dir name, are you sure directory exist ? – jmj Jul 11 '14 at 20:21
  • Check the answer http://stackoverflow.com/questions/18144660/what-is-path-of-jdk-on-mac. – Federico Piazza Jul 11 '14 at 20:24
  • @JigarJoshi, it's the early access version from [here](https://jdk8.java.net/download.html). – Kat Jul 11 '14 at 20:39
  • does it have java compiler `ls -la /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/bin/javac` ? – jmj Jul 11 '14 at 20:40
  • Have you saw this http://stackoverflow.com/questions/2503658/specify-jdk-for-maven-to-use? – Diogo Moreira Jul 11 '14 at 20:44
  • @Fede I believe that the value for JAVA_HOME is correct. `which java` returns `/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/bin/java`, which is the same path being used for JAVA_HOME. – Kat Jul 11 '14 at 20:44
  • can you `cat` your `mvn` is it resetting `JAVA_HOME` ? – jmj Jul 11 '14 at 20:44
  • @JigarJoshi, output is [here](http://pastebin.com/8SYj4XQa). – Kat Jul 11 '14 at 20:47
  • can you try echoing it right before java invocation ? just to verify – jmj Jul 11 '14 at 20:51
  • @JigarJoshi, I found the value being overridden in `~/.mavenrc` (noticed that when I was inspecting the source of `mvn`). – Kat Jul 11 '14 at 20:57
  • @Mike that was tricky (I added it as an answer) – jmj Jul 11 '14 at 20:58
  • @hd1, I think my answer was the best fit (it actually solved it, although JigarJoshi's answer probably would have found the issue, too). However, I cannot accept my own answer for 2 days. Although I'm also open to other answers, if there's a "better way" to avoid these kinds of problems (values being set in multiple places). – Kat Jul 11 '14 at 21:06
  • Set it on command line -- `env JAVA_HOME=/usr/local/ /path/to/maven/bin/mvn` -- the problem is that you then need to remember to add it in every time. – hd1 Jul 11 '14 at 21:26
  • i 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 06 '16 at 18:31
  • For me, upgrading from Maven 3.2.5 to 3.3.3 did the trick. – Benj Jun 27 '16 at 13:53
  • Similar problem and had JDK 1.8 / JAVA_HOME set to 1.8 / eclipse.ini pointed to JDK 1.8, still could not get it worked. Finally, ran mvn install from command line and voila, it works. That means m2e requires some tweak in configurations I reckon. Need to do some work to get this working in eclipse. – Anand Feb 03 '17 at 05:24

9 Answers9

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
Community
  • 1
  • 1
Kirby
  • 15,127
  • 10
  • 89
  • 104
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
Kat
  • 4,645
  • 4
  • 29
  • 81
  • 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). – Jakub Kotowski Feb 19 '15 at 12:42
  • If you can unset it, you should also be able to simply override the existing value. – Kat Feb 19 '15 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: https://bugs.launchpad.net/ubuntu/+source/java-common/+bug/45348 – Jakub Kotowski Feb 19 '15 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.

Bevor
  • 8,396
  • 15
  • 77
  • 141
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
Abdull
  • 26,371
  • 26
  • 130
  • 172
  • 2
    in my case it was **sudo update-alternatives --config javac** Are you sure "alternatives" is not a typo? – PbxMan Mar 13 '17 at 09:10
  • `update-alternatives` is relevant to Debian and Ubuntu, whereas `alternatives` is relevant to CentOS/RedHat. – Paulie-C Sep 13 '19 at 10:07
14

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

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 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 '14 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 '15 at 13:45
  • I agree with @scravy - ```export JAVA_HOME=$(/usr/libexec/java_home -v1.8)``` – joensson Feb 06 '15 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>
Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48
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.

mathielo
  • 6,725
  • 7
  • 50
  • 63
sandeep pandey
  • 350
  • 1
  • 10
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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 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 https://jdk8.java.net/download.html. And it works with Eclipse (I tested a lambda). – Kat Jul 11 '14 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 '14 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 '14 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 '14 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 '14 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.

Henno Vermeulen
  • 1,495
  • 2
  • 15
  • 25