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.

+3


source to share


3 answers


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.

+1


source


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 manifest use.newer.java.Version8

    or use.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.


https://github.com/rtyley/use-newer-java

+1


source


Distribute your application via JWS . This is to ensure that the correct version of the JRE is installed.

0


source







All Articles