3

If the Java version is to old then Java produce such very cryptic error message: UnsupportedClassVersionError... . Although we have the necessary Java version in the release notes, many customers contact the support with this error message.

Is there a simple solution to show the users a better error message?

We use Gradle for the building. How can I compile a small set of files with a lower class file version? In this files we can do a version check to display the error message.

Of course we can't change the class file version of the completely project.

Horcrux7
  • 23,758
  • 21
  • 98
  • 156
  • does this help http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime – Scary Wombat Apr 13 '17 at 06:17
  • Exactly - to add on that, you can make one Java class that is run either at install time or every time that you start the program, that is compiled for JDK 1.1, and that checks the version of Java - if it's too old, you show a user-friendly error message. – Erwin Bolwidt Apr 13 '17 at 06:21

3 Answers3

1

How can I compile a small set of files with a lower class file version?

Use the -source and -target options of the javac command (see the reference documentation), or since you're using Gradle, use the appropriate corresponding settings in the Gradle build file for the part that you need to be able to run on an older Java version.

For Gradle you can use the sourceCompatibility and targetCompatibility properties, see Table 47.8 in the Gradle documentation.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • If I set this properties to a lower value this has an effect to all files. This does not work. – Horcrux7 Apr 13 '17 at 09:00
  • @Horcrux7 Don't put that in the main Gradle config file for the whole project of course. Create a separate module and configure this only for that module. – Jesper Apr 13 '17 at 09:13
1

I've created a small library called use-newer-java which you can use to perform the version check:

  1. Include use-newer-java as a dependency in your project
  2. Set the Main-Class: field in the manifest of your built jar to use.newer.java.Version8 or use.newer.java.Version9, etc as appropriate.
  3. Set the Main-Class-After-UseNewerJava-Check: in your jar manifest to the normal main class of your app - Use Newer Java will follow this setting, and invoke this code after making it's check.

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

Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
0

Distribute your application via JWS. It ha provisions for ensuring that the required JRE version is installed.

Jolta
  • 2,620
  • 1
  • 29
  • 42
user207421
  • 305,947
  • 44
  • 307
  • 483