156

My pom file lists

<project>
  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  ...

However upon mvn clean install, I get

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing javac, but could not parse the error:
javac: invalid target release: 1.7
Usage: javac <options> <source files>

/usr/bin/java -version is (which java points here)

java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

javac also points to the correct Java version

/usr/bin/javac -> /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home/bin/javac

On this machine, I am using zsh (echo $0 returns -zsh)

In my .zshrc, I have defined:

 33 # HOME
 34 JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home
 35 SCALA_HOME=/Library/Scala/current
 36 FORGE_HOME=~/tools/forge/
 37 
 38 # PATH
 39 PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
 40 PATH=${PATH}:${JAVA_HOME}/bin
 41 PATH=${PATH}:/bin/
 42 PATH=${PATH}:/sbin/
 43 PATH=${PATH}:/usr/bin/
 44 PATH=${PATH}:/usr/sbin/
 45 PATH=${PATH}:/opt/local/bin/
 46 PATH=${PATH}:/opt/local/sbin/
 47 PATH=${PATH}:/usr/local/git/bin
 48 PATH=${PATH}:/usr/local/git/sbin
 49 PATH=${PATH}:/Applications/Xcode.app/Contents/Developer/usr/bin
 50 PATH=${PATH}:${SCALA_HOME}/bin
 51 PATH=${PATH}:${FORGE_HOME}/bin
 52 
 53 export PATH

When I am running mvn clean install --debug I see that in fact I use Java 6

  1 Apache Maven 3.0.3 (r1075438; 2011-02-28 11:31:09-0600)
  2 Maven home: /usr/share/maven
  3 Java version: 1.6.0_35, vendor: Apple Inc.
  4 Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Where could it be defined? I have source(d) my .zshrc multiple times.

7
  • 2
    Your PATH doesn't have /usr/bin first in it's list of directories. Try javac -version without specifying a directory. Dec 26, 2012 at 16:09
  • @PeterLawrey, i get javac 1.7.0_10 Dec 26, 2012 at 16:11
  • 2
    What does echo $JAVA_HOME tell you? Dec 26, 2012 at 16:12
  • 2
    What about your JAVA_HOME variable? mvn will use JAVA_HOME. Also, are you building from command line or inside IDE?
    – Lucas
    Dec 26, 2012 at 16:12
  • 8
    If you run Maven with --debug, it should tell you the exact command line it is using to run javac (along with tons of other stuff). What does it say?
    – Emil Sit
    Dec 26, 2012 at 16:12

18 Answers 18

153

Check the mvn script in your maven installation to see how it's building the command. Perhaps you or someone else has hard-coded a JAVA_HOME in there and forgotten about it.

8
  • 8
    You sir are the winner. Indeed someone hardcoded JAVA_HOME inside the script! THANK YOU Dec 26, 2012 at 17:02
  • 21
    Heh, not that I've ever done that to myself before or anything... :) Dec 26, 2012 at 17:56
  • 4
    The mvn script wasn't compatible with my OSX installation of Oracle's JDK 7, and I didn't do anything fancy setting it up (the script looks for a folder Library/Java/JavaVirtualMachines/CurrentJDK which didn't exist for me (what did exist is jdk1.7.0_25.jdk instead of CurrentJDK). Not an elegant fix but I just hardcoded the export and now it works (PS: maven on OSX is at /usr/share/maven/bin/mvn)
    – Raekye
    Aug 4, 2013 at 6:55
  • 6
    OSX 1.9.2 mavericks, with maven installed via homebrew, the mvn script was located at /usr/local/bin/mvn Mar 19, 2014 at 11:45
  • 1
    I had a JAVA_HOME set and this gave the clue. THANKS!! :D Apr 8, 2014 at 7:05
110

try using a newer version of the maven compiler plugin:

    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>

also, specifying source file encoding in maven is better done globally:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

EDIT: As this answer is still getting attention i'd just like to point out that the latest values (as of latest edit) are 3.2 for maven compiler plugin and 1.8 for java, as questions about compiling java 8 code via maven are bound to appear soon :-)

4
  • Adding "<version>3.0</version>" fixed the problem.
    – Sri
    Dec 27, 2013 at 15:18
  • @radai, its 3.2 now, but silly to keep updating as it will never stop increasing...
    – Lucas
    Nov 16, 2014 at 19:06
  • @Lucas - just dont want to be responsible for people sticking to outdated versions, is all. couldnt live with myself :-)
    – radai
    Nov 20, 2014 at 8:58
  • Why does this answer have almost a hundred upvotes when it is not the correct solution to the question posed? I'm lost...
    – Zero3
    Jan 9, 2016 at 4:40
48

I had the same problem and to solve this I follow this blog article: http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/

$ vim .bash_profile 

export JAVA_HOME=$(/usr/libexec/java_home)

$ source .bash_profile

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

special tks to @mkyong

EDIT: Now I'm using: jEnv + sdkman

3
  • 3
    This worked for me. I am using a mac, with maven from macports and the sun jdk. Apparently that combo was tricky.
    – Quantum7
    May 5, 2014 at 14:03
  • 1
    Great work. Thank you for all the commands! Works now on Maveriks
    – Maksim
    Aug 30, 2014 at 3:05
  • 1
    This is the easiest and most elegant solution. Thank you!
    – BK-
    Feb 24, 2015 at 1:08
26

Please check you pom.xml for the below tags

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

it should point the required jdk version

19

You have to check Maven version:

mvn -version

You will find the Java version which Maven uses for compilation. You may need to reset JAVA_HOME if needed.

19

I had the same problem. I found that this is because the Maven script looks at the CurrentJDK link below and finds a 1.6 JDK. Even if you install the latest JDK this is not resolved. While you could just set JAVA_HOME in your $HOME/.bash_profile script I chose to fix the symbolic link instead as follows:

ls -l /System/Library/Frameworks/JavaVM.framework/Versions/
total 64
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 30 Oct 16:18 1.6.0 -> CurrentJDK
drwxr-xr-x  9 root  wheel  306 11 Nov 21:20 A
lrwxr-xr-x  1 root  wheel    1 30 Oct 16:18 Current -> A
lrwxr-xr-x  1 root  wheel   59 30 Oct 16:18 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

Notice that CurrentJDK points at 1.6.0.jdk

To fix it I ran the following commands (you should check your installed version and adapt accordingly).

sudo rm /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
1
  • If mvn clean install -debug shows java 1.6 this is the correct answer
    – cetnar
    May 17, 2014 at 22:42
4

Diagnostics:

You can see which java version Maven uses by running "mvn --version"

Solution for Debian:

The mvn script sets the JAVA_HOME env variable internally by looking for javac (which javac). Therefore, if you have multiple java versions installed concurrently, e.g. JDK 6 and JDK 7 and use the Debian Alternatives system to choose between them, even though you changed the alternative for "java" to JDK 7, mvn will still use JDK 6. You have to change the alternative for "javac", too. E.g.:

# update-alternatives --set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac

EDIT:

Actually, an even better solution is to use update-java-alternatives (e.g.)

# update-java-alternatives -s java-1.7.0-openjdk-amd64

as detailed in https://wiki.debian.org/JavaPackage, because this will change all the alternatives to various Java tools (there's a dozen or so).

1
  • Thanks, I was confused after setting the java version with: update-alternatives --config javac Jul 10, 2014 at 9:00
2

Could you try a newer plugin; on the maven site:

<version>3.0</version>

I saw the following too:

<compilerVersion>1.7</compilerVersion>
6
  • Does not work. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project divs: Fatal error compiling: invalid target release: 1.7 - Dec 26, 2012 at 16:15
  • I've successfully used 2.3.2 to compile 1.7 before... But always good to use the latest if possible.
    – Lucas
    Dec 26, 2012 at 16:15
  • Searched further. Check your dependency plugin management if you use that.
    – Joop Eggen
    Dec 26, 2012 at 16:18
  • Added <pluginDependency>, same issue Dec 26, 2012 at 16:24
  • If it is a module project, the parent project might be worth looking into. This cannot be the case here. Is maven running with java 1.7?
    – Joop Eggen
    Dec 26, 2012 at 16:40
2

Try to change Java compiler settings in Properties in Eclipse-

Goto: Preferences->Java->Compiler->Compiler Compliance Level-> 1.7 Apply Ok

Restart IDE.

Confirm Compiler setting for project- Goto: Project Properties->Java Compiler-> Uncheck(Use Compliance from execution environment 'JavaSE-1.6' on the java Build path.) and select 1.7 from the dropdown. (Ignore if already 1.7)

Restart IDE.

If still the problem persist- Run individual test cases using command in terminal-

mvn -Dtest=<test class name> test
2

Not sure what the OS is in use here, but you can eliminate a lot of java version futzing un debian/ubuntu with update-java-alternatives to set the default jvm system wide.

#> update-java-alternatives -l
java-1.6.0-openjdk-amd64 1061 /usr/lib/jvm/java-1.6.0-openjdk-amd64
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-6-sun 63 /usr/lib/jvm/java-6-sun
java-7-oracle 1073 /usr/lib/jvm/java-7-oracle

To set a new one, use:

#> update-java-alternatives -s java-7-oracle

No need to set JAVA_HOME for most apps.

2

right click on ur project in eclipse and open "Run Configurations"..check the jre version there. some times this will not change by default in eclipse,after even changing the version in the buildpath.

0
2

For a specific compilation that requires a (non-default /etc/alternatives/java) JVM, consider prefixing the mvn command with JAVA_HOME like this,

JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/ mvn package

Here we assume the default is Java 8, whereas for the specific project at hand we require Java 7.

2
  • This worked best for me as then there does not need to be changes performed to the source of the project6 downloaded from GIT (or SCM)
    – jwilleke
    Sep 29, 2016 at 16:48
  • This answer deserves more upvotes. It helped me compile a code requiring Java 7 on my machine where default was Java 8 - without having to install and re-install Java7 and Java 8.
    – R11G
    Nov 19, 2019 at 3:53
1

{JAVA_1_4_HOME}/bin/javacyou can try also...

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <executable>{JAVA_HOME_1_7}/bin/javac</executable>
                <fork>true</fork>
        </configuration>
    </plugin>
2
  • 4
    You should not hardcode the path on any code that will ever be shared.
    – Jan Segre
    Nov 7, 2013 at 17:13
  • @Jan Segre, ok i change it :)
    – bora.oren
    Jan 8, 2014 at 12:07
0

Ok, I just solved this issue on my own too. It is more important your JAVA_HOME, if you don't have a lower or no version compared to source/target properties from the Maven plugin, you will get this error.

Be sure to have a good version in your JAVA_HOME and have it included in your PATH.

0

You might be specifying a wrong version of java. java -version(in your terminal) to check the version of java you are using. Go to maven-compile-plugin for the latest maven compiler version Your plugin may appear like this if you are using java 6 and the latest version of maven compiler plugin is 3.1

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
0

None of the previous answers completely solved my use case.

Needed to remove the directory that was being built. Clean. And then re-install. Looks like a silent permissions issue.

0

I had this problem in IntelliJ IDEA 14 until I went into File menu --> Project Structure, changing project SDK to 1.7 and project language level to 7.

0

I had this problem when working with eclipse, I had to change the project's build path so that it refers to jre 7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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