Explain Codes LogoExplain Codes Logo

Intellij idea - Error: java: invalid source release 1.9

java
intellij
java-configuration
error-handling
Anton ShumikhinbyAnton Shumikhin·Nov 13, 2024
TLDR

To rectify the Error: java: invalid source release 1.9, you'll need to set your IntelliJ IDEA JDK to Java 9 or higher:

  1. Launch Project Structure (Ctrl+Alt+Shift+S).
  2. Under Project SDK, switch to a JDK 9+, or add it via Add SDK if it's absent.
  3. Alter the Project language level to 9 - Modules.

Quick config:

Project Structure (Ctrl+Alt+Shift+S) > Project SDK: Java 9+ > Language Level: 9

Afterwards, rebuild your project to implement the JDK update.

Unraveling module complexities

If the error persists despite successfully setting the JDK and language level, it means you need to dig a bit deeper. Inspect your module settings:

  1. In Project Structure, navigate to Modules.
  2. Choose the relevant module, select the Sources tab, and review the language level.
  3. Verify that the module's bytecode version aligns with your target. Set it to 1.8 if necessary.
  4. Repeat these steps for every module in your project.

Build tool specifics: Maven and Gradle

For Maven projects, make sure that your pom.xml is equipped with the appropriate Java version in its <properties>:

<!-- Maven, armed with the wisdom of foresight --> <properties> <maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.target>1.9</maven.compiler.target> </properties>

On the other hand, while dealing with a Gradle project, head over to your build.gradle file and modify the sourceCompatibility:

// Gradle and Java 1.9, a match made in heaven sourceCompatibility = '1.9'

Checklist and additional guidance

If you've followed the steps, and yet you're still staring at the error message on your screen, here's a quick checklist and a bit more guidance:

SDK: Aligned with project requirements

  • Assert that the correct SDK is set in SDK Manager within the IntelliJ environment. The selected SDK should be Java 9 or beyond.
  • Validate the Java SDK's installation and inclusion in the SDK list.

Code review: Ensuring compatibility

  • If your project is set to Java 1.8, take extra care: ensure your code doesn't contain features exclusive to Java 1.9.
  • Invoke IntelliJ's code inspection to unearth language-level compatibility nuances.

Parser library update

  • If you're using the JSQLParser library (or similar), consider updating to a version compatible with your Java version.

Project Structure revisited

  • Head over to File > Settings > Compiler. Ensure the project bytecode version is matching your target.

Gradle JVM version fit for the job

  • For Gradle projects, navigate to File > Settings or Preferences (for macOS users), then find Build, Execution, Deployment > Build Tools > Gradle, and set the Gradle JVM version appropriately.

Module SDK: Consistent with Java version

  • Don't forget to set the Module SDK in the Modules section of Project Structure to the intended Java version.

Corner cases and nuanced scenarios

Plenty of corner cases crop up. Here's how to deal with a few common ones:

SDK and plugin updates

Frequent software updates are the reality of today's development environment. Keep your IDE, SDKs, and plugins updated to avoid mismatches.

Multi-module projects: A test for the best

Multi-module projects are a bed of potential issues. Make sure your module settings are consistent across the board.

CI servers: The hidden trap

If you're using a CI/CD environment, ensure the Java version on the server matches your project settings.