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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
James Raitsev
  • 92,517
  • 154
  • 335
  • 470

18 Answers18

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 8
    You sir are the winner. Indeed someone hardcoded JAVA_HOME inside the script! THANK YOU – James Raitsev Dec 26 '12 at 17:02
  • 21
    Heh, not that I've ever done that to myself before or anything... :) – Ryan Stewart Dec 26 '12 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 04 '13 at 06:55
  • in ubuntu 12.04 the mvn script is at /etc/alternatives. Unfortunately it does not recognize the result of sudo update-alternatives --config java - but setting export JAVA_HOME= to the currently choosen result home part works – Wolfgang Fahl Feb 12 '14 at 17:53
  • 6
    OSX 1.9.2 mavericks, with maven installed via homebrew, the mvn script was located at `/usr/local/bin/mvn` – StackExchange What The Heck Mar 19 '14 at 11:45
  • 1
    I had a JAVA_HOME set and this gave the clue. THANKS!! :D – Alfonso Nishikawa Apr 08 '14 at 07:05
  • I was using JAVA_HOME=, but in macosx we need to specify, export JAVA_HOME= and then mvn picked up correct path – Sn. Aug 06 '15 at 19:37
  • I can't believe that, my java_home was right but oracle put at the beginning a shortcuts paths at the beginning of `PATH` variable. I deleted it okay to all windows and opened a clear command prompt , it's done now. – Davut Gürbüz Jul 14 '16 at 22:47
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 :-)

Justin Kaeser
  • 5,868
  • 27
  • 46
radai
  • 23,949
  • 10
  • 71
  • 115
  • Adding "3.0" fixed the problem. – Sri Dec 27 '13 at 15:18
  • @radai, its 3.2 now, but silly to keep updating as it will never stop increasing... – Lucas Nov 16 '14 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 '14 at 08: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 09 '16 at 04: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

eliocapelati
  • 699
  • 6
  • 11
27

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

Aritz
  • 30,971
  • 16
  • 136
  • 217
user4010880
  • 271
  • 3
  • 2
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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Loi Cao
  • 503
  • 4
  • 7
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
Chris McCarthy
  • 191
  • 1
  • 3
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).

user323094
  • 3,643
  • 4
  • 21
  • 30
2

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

<version>3.0</version>

I saw the following too:

<compilerVersion>1.7</compilerVersion>
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 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 -` – James Raitsev Dec 26 '12 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 '12 at 16:15
  • Searched further. Check your dependency plugin management if you use that. – Joop Eggen Dec 26 '12 at 16:18
  • Added , same issue – James Raitsev Dec 26 '12 at 16:24
  • If it is a module project, the parent project might be worth looking into. [This](http://stackoverflow.com/questions/5113030/setting-the-source-and-target-of-the-java-compiler-with-maven-doesnt-work) cannot be the case here. **Is maven running with java 1.7?** – Joop Eggen Dec 26 '12 at 16:40
  • [java-version-mismatch-in-maven](http://stackoverflow.com/questions/11112721/java-version-mismatch-in-maven) – Joop Eggen Dec 26 '12 at 16:43
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
Mithun Khatri
  • 636
  • 3
  • 9
  • 22
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.

Bruce Edge
  • 1,975
  • 1
  • 23
  • 31
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.

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.

elm
  • 20,117
  • 14
  • 67
  • 113
  • 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 '16 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 '19 at 03: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>
bora.oren
  • 3,439
  • 3
  • 33
  • 31
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.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
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>
Henry
  • 91
  • 1
  • 5
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.

Piper
  • 1,266
  • 3
  • 15
  • 26
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.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
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

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47