Explain Codes LogoExplain Codes Logo

Fatal error compiling: invalid target release: 1.8 ->

java
maven-compiler-plugin
java-8
environment-variables
Alex KataevbyAlex Kataev·Feb 7, 2025
TLDR

Fix the Java 1.8 compile error ASAP by configuring your JDK to version 1.8. In Maven, use this:

<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>

For special Gradle fans, update your build.gradle like this:

sourceCompatibility = '1.8' // setting the bar as high as 8! targetCompatibility = '1.8' // aiming as high as we set!

Make sure JAVA_HOME has the correct direction to JDK 1.8, and run your build with the finest JDK.

Making Environment Variables Your Friends

Getting JAVA_HOME and Maven's POM file to play ball is crucial to the build process. If they don't, you're in for a rough innings with Maven.

To shine the light on your path to victory (read: set environment variables):

  • Windows:
    • Like your favorite shopping routine: Right-click -> Properties -> Advanced System Settings -> Fill cart (Environment Variables)
    • Rewrite JAVA_HOME to guide it to your JDK 1.8 lair
  • Unix-like systems (Linux, macOS):
    • Get ~/.bashrc or ~/.zshrc to confess (export JAVA_HOME=<path-to-jdk-1.8>)

Maven and Java: A Love Story

A mismatch between Maven and the JDK version is a recipe for a disaster--well, fatal error at least. Make sure the maven-compiler-plugin is dating the right JDK version.

This is what your maven-compiler-plugin should be whispering:

<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- right here, make sure it's someone Java 1.8 can get along with --> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>

Plan B: When JDK 1.8 is the One That Got Away

If you and JDK 1.8 are not meant to be, simply adjust the target release in your Maven POM or Gradle build script to comfort your JDK version.

Say No to JDK-JRE Contradictions

Eclipse IDE users, listen up! If the "invalid target release" looms over you, here's the path: Window -> Preferences -> Java -> Installed JREs. Make sure it's JDK 1.8 calling the shots, not just the JRE.

When Version Mismatch Throws Tantrums

If java -version and javac -version are like siblings not getting along, it's likely your PATH is serving different cuisines. Update it to include the JDK 1.8 bin directory.

Can't make peace between the versions?

  • Temporarily expel the rebel versions using unset command.
  • Permanently exile conflicting PATH entries by editing config files (~/.bashrc or ~/.zshrc)

SDKMAN! to the Rescue

Referee your SDK versions with SDKMAN!. It's super handy to switch Java SDK versions sans manual tweaks.

Use these spells to install JDK 1.8 and switch:

sdk install java 8.0.x-jdk // it's like magic, trust me! sdk use java 8.0.x-jdk // just say the word!