Maven "invalid target release: 11" Error: Causes and Solutions
The "Fatal error compiling: invalid target release: 11" error occurs when Maven attempts to compile Java code with Java 11 as the target, but the underlying Java runtime doesn't support it. This typically happens when your environment variables, IDE settings, or Maven configuration point to incompatible Java versions.
Root Cause Analysis
The core issue is a mismatch between:
- The Java version specified in your
pom.xml(Java 11) - The Java version Maven actually uses during compilation (often Java 8)
This is evident when java -version shows Java 11, but mvn --version shows Java 8.
Complete Solutions
1. Configure JAVA_HOME Environment Variable
The most common fix is ensuring your JAVA_HOME environment variable points to JDK 11:
Windows:
set JAVA_HOME=C:\Program Files\Java\jdk-11.0.8
set PATH=%JAVA_HOME%\bin;%PATH%Linux/macOS:
export JAVA_HOME=/path/to/jdk-11
export PATH=$JAVA_HOME/bin:$PATHVerify the changes:
echo %JAVA_HOME% # Windows
echo $JAVA_HOME # Linux/macOS
mvn --version # Should show Java 11WARNING
Restart your terminal/IDE after changing environment variables for changes to take effect.
2. Update Maven Compiler Plugin Configuration
Ensure your pom.xml uses the correct compiler configuration. The modern approach uses the release parameter:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>The release parameter is preferred over separate source and target as it automatically configures the correct boot classpath.
3. IDE-Specific Configuration
IntelliJ IDEA:
- File → Project Structure → Project → Project SDK: Set to JDK 11
- File → Settings → Build, Execution, Deployment → Build Tools → Maven → Runner: Set JRE to JDK 11
Eclipse:
- Window → Preferences → Java → Installed JREs: Add JDK 11
- Right-click project → Properties → Java Build Path → Libraries: Ensure JRE System Library points to JDK 11
- For Maven executions: Run Configurations → JRE tab → Select JDK 11
4. Using SDKMAN! (Multi-JDK Management)
For developers working with multiple Java versions, SDKMAN! provides an elegant solution:
# Install SDKMAN!
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# List available JDKs
sdk list java
# Install and use JDK 11
sdk install java 11.0.11.hs-adpt
sdk use java 11.0.11.hs-adpt
# Set as default
sdk default java 11.0.11.hs-adptCreate a .sdkmanrc file in your project for automatic version switching:
java=11.0.11.hs-adpt5. Verify Maven Configuration
Check where Maven is looking for Java by examining the mvn or mvn.bat script. Look for hardcoded JAVA_HOME references that might override your environment variable.
Common Pitfalls and Troubleshooting
DANGER
Avoid downgrading your pom.xml to Java 8 as a "solution." This bypasses the real issue and prevents you from using Java 11 features.
Check your PATH variable order: Ensure the JDK 11 bin directory appears before any other Java installations in your PATH.
Verify Maven is using the correct JDK: The output of mvn --version should match your expected Java version.
Check for multiple Maven installations: Remove or update any additional Maven installations that might be using older Java versions.
Clean and rebuild: After making changes, run:
mvn clean compilePrevention Best Practices
- Use consistent Java versions across development, testing, and production environments
- Leverage version management tools like SDKMAN! or Jabba for multiple JDK projects
- Set up continuous integration to catch environment mismatches early
- Document required JDK version in your project README
- Use Docker containers for consistent build environments in complex setups
By systematically addressing the version mismatch through proper environment configuration and build tool settings, you can resolve the "invalid target release" error and successfully compile your Java 11 projects with Maven.