Compiling Java 7 code via Maven

JavaMaven

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

<version>3.0</version>

I saw the following too:

<compilerVersion>1.7</compilerVersion>

Solution 9 - Java

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

Solution 10 - Java

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.

Solution 11 - Java

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.

Solution 12 - Java

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.

Solution 13 - Java

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

Solution 14 - Java

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.

Solution 15 - Java

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][1] for the latest maven compiler version [1]: http://maven.apache.org/plugins/maven-compiler-plugin/ 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>

Solution 16 - Java

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.

Solution 17 - Java

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.

Solution 18 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames RaitsevView Question on Stackoverflow
Solution 1 - JavaRyan StewartView Answer on Stackoverflow
Solution 2 - JavaradaiView Answer on Stackoverflow
Solution 3 - JavaeliocapelatiView Answer on Stackoverflow
Solution 4 - Javauser4010880View Answer on Stackoverflow
Solution 5 - JavaLoi CaoView Answer on Stackoverflow
Solution 6 - JavaChris McCarthyView Answer on Stackoverflow
Solution 7 - Javauser323094View Answer on Stackoverflow
Solution 8 - JavaJoop EggenView Answer on Stackoverflow
Solution 9 - JavaMithun KhatriView Answer on Stackoverflow
Solution 10 - JavaBruce EdgeView Answer on Stackoverflow
Solution 11 - Javaravinder reddyView Answer on Stackoverflow
Solution 12 - JavaelmView Answer on Stackoverflow
Solution 13 - Javabaybora.orenView Answer on Stackoverflow
Solution 14 - JavaSilviu BurceaView Answer on Stackoverflow
Solution 15 - JavaHenryView Answer on Stackoverflow
Solution 16 - JavaPiperView Answer on Stackoverflow
Solution 17 - JavaJon OnstottView Answer on Stackoverflow
Solution 18 - Javaosama yaccoubView Answer on Stackoverflow