This article is meant as an extension to Angie Jones’ “IntelliJ – Error:java: release version 5 not supported”.
In her article Angie suggest solutions to IntelliJ IDEA showing the error message “release version 5 not supported”. The first two options work within IDEA, but are not sustainable as you will learn later. The last option tackles the actual problem, but only for Maven based builds. To recap, the correct solution for Maven based builds (see also Maven Compiler Plugin), is as follows:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I would like to give some background on the problem and suggest to solve the problem differently.
What is the actual problem?
To find a proper solution for all kind of Java projects, regardless of the build tool, let’s dive into the actual problem.
In its more than 20 years, Java went through several major versions. Each of those introduced a new version of the binaries format and sometimes also language features (e.g. enums in Java 5, lambdas in Java 8, modules in Java 9, var in Java 10, HTTP-2 client Java 11… ). Yet, Java and its virtual machine are highly backwards-compatible. You can take 20 years old binaries and they will run with the latest version of Java just fine (exceptions apply).
The Java compiler, which generates binary files from Java source files supports cross-compiling to various language levels, that is creating these binaries in formats of different Java versions. However, sometimes support for older language levels is dropped. Since Java 9, the compiler cannot generate Java 5 binaries anymore. If the compiler now runs – directly or indirectly through a build tool – with a target version of Java 5 it will show the error message release version 5 not supported.
The root problem for the error message is, that the Java Development Kit (JDK) is too new to generate old binaries. |
---|
Proper Solutions
So, your JDK is too new to generate Java 5 binaries. There are two solutions to tackle this problem.
Use an older JDK
As previously mentioned, JDK 9 dropped the support for cross-compiling to Java 5 binaries. This means, as long as you use a JDK 8 or below, you can actually create Java 5 binaries. However, this method is NOT recommended. There should be no reason to stick to Java 5 in a production runtime environment. JDK 5 is not supported since 2015 and security patches are not provided anymore.
Change the desired target language level
Instead of cross-compiling to Java 5 binaries, you should create binaries, that match the version of the Java runtime environment in production. This should be at least Java 8 until 2023 to get security patches. See also Java version history on Wikipedia. However, I recommend to use Java 11 to make use of various new features introduced in-between and staying on an LTS version at the same time.
So, how to change the target version then?
Maven-based projects
For very good reasons Maven is still vastly used in the Java ecosystem. However, Maven is quite old and so are some of the defaults as well. One of which is the language level, that defaults to Java 1.5, regardless of the JDK you use.
In plain Maven projects you can paste the above mentioned code into your build configuration file pom.xml to set the target language level.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
In SpringBoot projects, the code slightly changes to
<properties>
<java.version>1.8</java.version>
</properties>
After changing the pom.xml you need to Reimport All Maven Projects in IntelliJ IDEA. The easiest way to do it, is to press cmd+shift+A (shift+ctrl+A on Windows/Linux) and then simply start typing.
Plain IntelliJ IDEA projects
If you did not choose any build system for your Java projects in IntelliJ, you simply need to make sure, the language level is configured according to the project’s JDK. You can do this in the project structure settings. Press cmd+shift+A and start typing to open the settings.
As soon, as you use an JDK 9+ you need to change the target level accordingly.
- Java 9-11: Target level Java 6
- Java 12+: Target level Java 7
Conclusion
In this post I explained the root cause for the error message release 5 version not supported.
One can work around this problem locally by changing IntelliJ project settings. However, if you wanted to solve the problem properly, you need to change the project’s build configuration file. Otherwise on the next import into IntelliJ you will need to change it again. Also, the build might behave differently on each computer. The CI server and others, who didn’t change their IntelliJ IDEA settings, will continue to produce Java 5 binaries. Persisting the language level in the build configuration file will produce stable and foreseeable results across machines.
Top comments (2)
I'm using IntelliJ 2018 and the only thing that worked was to edit .idea/compiler.xml and adjust bytecodeTargetlevel:
<bytecodeTargetLevel>
<module name="resource-server" target="17" />
</bytecodeTargetLevel>
I appreciate the deeper insight on the issue.