1

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?

Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Nov 20, 2012 at 0:10
3
  • Just to clarify, you mean a user running JRE N trying to run a java program compiled with JDK >N, right? Commented 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. Commented Nov 20, 2012 at 2:03
  • Don't bother using Java 7 if you want your code to run anywhere else. Commented Nov 20, 2012 at 2:04

1 Answer 1

3

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.

answered Nov 20, 2012 at 1:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.