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?
-
Just to clarify, you mean a user running JRE N trying to run a java program compiled with JDK >N, right?anthropomo– anthropomo2012年11月20日 00:38:21 +00:00Commented Nov 20, 2012 at 0:38
-
You can often specify a JRE target when you compile. Here's some more information: docs.oracle.com/javase/1.5.0/docs/tooldocs/solaris/javac.html Search for the target option. This would work, for example, if you build your project with JDK 6 and would like it to run on JRE 5.jahroy– jahroy2012年11月20日 02:03:25 +00:00Commented Nov 20, 2012 at 2:03
-
Don't bother using Java 7 if you want your code to run anywhere else.jahroy– jahroy2012年11月20日 02:04:32 +00:00Commented Nov 20, 2012 at 2:04
1 Answer 1
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:
It references system classes not available in the previous version of Java. In that case, you will see NoClassDefError.
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.