Karlovsky120
Karlovsky120

Reputation: 6362

Running java program in lower version JRE

How will program designed and compiled in JRE X act when it's being run in JRE Y, Y

Will it not run at all, terminate as soon as it's started, or will it run untill it comes across a method or class that doesn't exist in JRE Y and then crash?

If it's the latter, I could do a version check on startup and inform the user that he needs to update his Java in order to run the program (if he has JRE Y).

If that doesn't work, is there another way of doing it?

Upvotes: 1

Views: 938

Answers (1)

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

If a program is built with Java X and runs with Java > X, it will function without issues.

If a program is built with Java X and runs with Java < X, the program may function correctly or terminate unexpectedly at any point during execution when an incompatible program class is loaded.

A program class can be incompatible for two reasons:

  1. It references system classes not available in the previous version of Java. In that case, you will see NoClassDefError.

  2. It uses a language feature not available in the previous version of Java. In that case, you will see ClassFormatError.

You can implement a Java version checker by referencing data returned by System.getProperty( "java.version" ). To ensure that the checker itself doesn't fail to run, you should compile it with the lowest version of Java that you expect to encounter. You must further ensure that the check happens as early as possible and before making any code references to the rest of the application. This will likely mean using Java Reflection in the checker to call into the main program class upon the successful check.

Upvotes: 3

Related Questions