[java] Compiling Java 7 code via Maven

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.

This question is related to java maven

The answer is


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.


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

<version>3.0</version>

I saw the following too:

<compilerVersion>1.7</compilerVersion>

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

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


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.


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.


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).


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.


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

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 :-)


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.


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>

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.


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


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


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

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.