121

I have seen similar questions, but haven't yet found the answer.

Using maven compile, I get:

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

Here is the pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

Javac version:

javac 1.7.0_25

Java version:

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Any idea what it could be?

Karan
  • 14,824
  • 24
  • 91
  • 157

5 Answers5

159

You need to set JAVA_HOME to your jdk7 home directory, for example on Microsoft Windows:

  • "C:\Program Files\Java\jdk1.7.0_40"

or on OS X:

  • /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
Douglas Held
  • 1,452
  • 11
  • 25
Michał Nowak
  • 1,686
  • 1
  • 12
  • 17
  • 71
    On a Mac, where jdk6 and jdk7 are installed, this can be forced in ~/.profile: `export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)` – Hank Feb 10 '14 at 20:31
  • 3
    jdk8 can also be used to get jdk7 compliance – Wolfgang Fahl Feb 11 '14 at 14:21
  • 3
    remember to reload ~/.profile on the mac with: `source ~/.profile` – Caio Iglesias Jul 23 '15 at 17:40
  • This probably works for a lot of things but it's not enough for Maven and certainly not for the maven compiler plugin. Check Mike's answer to his own question here: [stackoverflow question 24705877](http://stackoverflow.com/questions/24705877/cant-get-maven-to-recognize-java-1-8) This solved the issue for me both command line AND within eclipse. Also, @LinGao answer to [stackoverflow question 2503658](http://stackoverflow.com/questions/2503658/specify-jdk-for-maven-to-use) and the use of the $JAVACMD variable might help. – Litome Oct 26 '15 at 15:46
42

Other than setting JAVA_HOME environment variable, you got to make sure you are using the correct JDK in your Maven run configuration. Go to Run -> Run Configuration, select your Maven Build configuration, go to JRE tab and set the correct Runtime JRE.

Maven run configuration

Stanley
  • 5,057
  • 4
  • 34
  • 44
  • +1, I knew I was forgetting to update something... The default option (for me) when creating a configuration was the "Alternate JRE". If anyone else runs across this, make sure your maven plugin AND configurations are updated properly! – king14nyr Jul 10 '14 at 18:05
  • I had the right JAVA_HOME for Java 8 but I was still lacking this. You solved my problem, thanks – dtortola Feb 17 '16 at 10:59
3

In IntelliJ IDEA this happened to me when I imported a project that had been working fine and running with Java 1.7. I apparently hadn't notified IntelliJ that java 1.7 had been installed on my machine, and it wasn't finding my $JAVA_HOME.

On a Mac, this is resolved by:

Right clicking on the module | Module Settings | Project

and adding the 1.7 SDK by selecting "New" in the Project SDK.

Then go to the main IntelliJ IDEA menu | Preferences | Maven | Runner

and select the correct JRE. In my case it updated correctly Use Project SDK, which was now 1.7.

user1978019
  • 3,008
  • 1
  • 29
  • 38
  • Note that there are Project Settings and Preferences for Intellij on a Mac at least, I had to go to Settings -> Build Exec, Deploy -> Java Compiler and then set the project bytecode version as needed. – Opentuned Jan 27 '15 at 16:35
1

When maven is working outside of Eclipse, but giving this error after a JDK change, Go to your Maven Run Configuration, and at the bottom of the Main page, there's a 'Maven Runtime' option. Mine was using the Embedded Maven, so after switching it to use my external maven, it worked.

djb
  • 1,635
  • 3
  • 26
  • 49
  • where is the Maven Run Configuration, please? – R Claven Jan 02 '15 at 23:05
  • 1
    Right click on your build.xml, go to 'Run as' and then 'Run Configuration' should be the last option. If you scroll through the types of run configs, one is 'Maven Build', and that has an option to choose the 'Maven Runtime'. There's a 'configure' button next to that – djb Jan 05 '15 at 09:54
0

This probably works for a lot of things but it's not enough for Maven and certainly not for the maven compiler plugin.

Check Mike's answer to his own question here: stackoverflow question 24705877

This solved the issue for me both command line AND within eclipse.

Also, @LinGao answer to stackoverflow question 2503658 and the use of the $JAVACMD variable might help but I haven't tested it myself.

Community
  • 1
  • 1
Litome
  • 653
  • 8
  • 12