How do I change the UnsupportedClassVersionError in a user friendly message?
If the Java version is old, then Java generates this very cryptic error message: UnsupportedClassVersionError .... Although the release notes contain the required Java version, many clients refer to it with this error message.
Is there an easy solution to show users the best error message?
We are using Gradle for the building. How can I compile a small set of files with a lower version of the class file? In these files, we can perform a version check to display an error message.
Of course, we cannot change the version of the class file of the entire project.
source to share
How can I compile a small set of files with a lower version of the class file?
Use the options -source
and -target
commands javac
(see the help documentation ) or, re with Gradle, use the appropriate settings in your Gradle build file for the part you need to run on the old Java version.
For Gradle, you can use properties sourceCompatibility
and targetCompatibility
, see Table 47.8 in Gradle documentation.
source to share
I created a small library called use-newer-java
that you can use to check the version:
- Include
use-newer-java
as a dependency in your project - Set the field
Main-Class:
to your inline jar manifestuse.newer.java.Version8
oruse.newer.java.Version9
etc if needed. - Set
Main-Class-After-UseNewerJava-Check:
in your manifest jar to your normal main class of your application. Using Newer Java will follow this parameter and call this code after validation.
source to share