javac: invalid target release: 1.8 on Mac when executing Maven command

18,154

Solution 1

First, figure out where 1.8 Java is installed by running the command:

/usr/libexec/java_home -v 1.8

Then, set your JAVA_HOME environment variable by running the command:

export JAVA_HOME=<whatever the output from the previous command was>

Maven should work afterwards, at least in that terminal window.

You'll have to set the JAVA_HOME environment variable in your profile if you don't want to have to run these commands every time you open a new terminal.

Solution 2

If you haven't done so already, use the maven-compiler-plugin to determine the Java version to use within Maven. Put this in your pom.xml file (change the <source/> and <target/> version to the JDK version you require):

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

(If you already have a <build/> and/or <plugins/> section, add the <plugin/> portion, only.)

Share:
18,154
Ragnarsson
Author by

Ragnarsson

Lemme think ...

Updated on July 22, 2022

Comments

  • Ragnarsson
    Ragnarsson almost 2 years

    I am trying to run automation test on Mac. I installed Maven and java, jdk as following:

    java version "1.8.0_25"
    Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
    Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
    

    and Maven:

    Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T18:29:23+01:00)
    Maven home: /usr/local/Cellar/maven/3.2.5/libexec
    Java version: 1.6.0_65, vendor: Apple Inc.
    Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    Default locale: en_US, platform encoding: MacRoman
    OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"
    

    When I executed Maven command, I got this error:

    [ERROR] Failure executing javac, but could not parse the error:
    [ERROR] javac: invalid target release: 1.8
    [ERROR] Usage: javac <options> <source files>
    [ERROR] use -help for a list of possible options
    [ERROR] -> [Help 1]
    

    I searched on here, there's one accepted solution is this:

    sudo cp $JAVA_HOME/lib/tools.jar /Library/Java/Extensions/
    

    I executed this command, but nothing happened! I don't know what's wrong.