[java] Fatal error compiling: invalid target release: 1.8 -> [Help 1]

Similar problem like one posted on invalid target release: 1.7 but after following the blog my problem is still unresolved.

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project hm_app: Fatal error compiling: invalid target release: 1.8 -> [Help 1]

I was following as tutorial when I faced this problem.

abt java & mvn

C:\mvn>echo %JAVA_HOME% 
C:\mvn>echo %JRE_HOME%
C:\mvn>echo %MAVEN_HOME% yields

outputs

C:\Program Files\Java\jdk1.7.0_51
C:\Program Files\Java\jre7
C:\apache-maven-3.0.4

This question is related to java maven

The answer is


Putting that in your .profile will dynamically take care of your $JAVA_HOME

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

Close your shell afterwards, open a new one and test with

echo $JAVA_HOME

It should display something like

/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home

If not, remove any other assignments of JAVA_HOME in your startup scripts. Remember, that these startup scripts start with a . so they are hidden and won't be included when using * wildcard, e.g. if you want to grep all files of your home directory, you need to:

grep -s JAVA_HOME ~/.* --exclude=.bash_history

Do a force Maven Update which will bring the compatible 1.8 Jar Versions and then while building, update the JRE Versions in Execute environment to 1.8 from Run Configurations and hit RUN


On Windows machine you can temporarily set Java version.
For example, to change the version to Java 8, run this command on cmd:

set JAVA_HOME=C:\\...\jdk1.8.0_65

In my case the maven "Run configuration" was using the wrong JRE (1.7). Be sure to check Run -> Run Configurations -> (Tab) JRE to be some jdk1.8.x.


As mentioned by Camila Macedo - you have to explicitly point the java version for compiler-plugin. For spring boot you can do that by next property:

  <properties>
    <java.version>1.8</java.version>
    <maven.compiler.release>8</maven.compiler.release>
  </properties>

I faced this issue deploying on Dokku, for some reason it was choosing JDK 1.7

Creating a system.properties file and setting java.runtime.version=1.8 solved the issue. Dokku now uses Java version 8. Choosing a JDK on Heroku

I Never had to do it before...


Using IntelliJ I just had to install another (higher) JDK Version. After restarting IDE, everything worked and even all dependencies were solved.


This question wasn't asking explicitly about Docker, but I received the same error when I had a pom.xml file that was targeting 1.9...

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.9</maven.compiler.source>
  <maven.compiler.target>1.9</maven.compiler.target>
</properties>

... but then tried to run tests against a Docker container by specifying "maven" by itself.

docker run -t --rm -v m2_repository:/root/.m2/repository -v $(pwd):/work -w /work maven mvn -e test

For me, the fix was to target the exact version I needed.

docker run -t --rm -v m2_repository:/root/.m2/repository -v $(pwd):/work -w /work maven:3.5.2-jdk-9 mvn test

(You can learn more here.)


The problem I was facing was that I was able to make a maven build from the command prompt but not from Eclipse.What worked for me in eclipse is that I changed the run configuration to point to JRE folder inside of JDK rather than leaving it in JDK folder only as per the standard.This solution may work for you as well but try this if and only if all the java paths are correct, java and javac are showing the same version as present in the target of pom.xml.


For eclipse here is how I solved my problem:

  1. Preferences --> Compiler --> Compiler Complainer Level (Change to 1.8) enter image description here

  2. Perferences --> Installed JREs --> select JAVA SE 8 1.8 enter image description here

  3. Rebuild via maven using Run as maven build.

It shouldn't show you the invalid target error anymore.
Note: I didn't have to set or change any other variables in my terminal. Hope this helps.


What worked in my case is this:

I opened the pom.xml and replaced the one of the plug-ins as below.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

Before I edited the source and target tags both had 1.8, I changed that to 1.7 and it worked.


In my case (IntelliJ), I needed to check if I had the right Runner for maven:

  1. Preferences
  2. Build, Execution, Deployment
  3. Maven -> Runner

Preferences


The issue was resolved as I was having a JDK pointing to 1.7 and JRE pointing to 1.8. Check in the command prompt by typing

java -version

and

javac -version

Both should be same.  


Put the value in the plugin:

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

The error was use:

<source>${java.version}</source>
<target>${java.version}</target>

If you are using Eclipse IDE then go inside Window menu and select preferences and there you search for installed JREs and select the JRE you need to build the project