Can newer JRE versions run Java programs compiled with older JDK versions?

Java

Java Problem Overview


Would I encounter any problems running Java programs and associated libraries compiled in Java version 1.6 and 1.7 (I'm compiling using 1.7 whereas some libraries are compiled using 1.6) and running the entire program in a 1.7 JRE?

Java Solutions


Solution 1 - Java

As answered already you are mostly safe and most products and 3rd party libraries will simply work. However there do exist very rare cases where binary incompatibilities (ones where the class file compiled using older JDK will fail to run in the newer JVM) were introduced between JDK versions.

Official list of Oracle Java incompatibilities between versions:

Compatibility tool

Packaged with JDK 9, there is a tool called [jdeprscan ][6] which will verify the compatibility, list no longer used APIs within your code and suggest alternatives(!). You can specify the target JDK version (works for JDK 9, 8, 7 and 6) and it will list incompatibilities specific to your target version.

Additional comment in case of libraries:

A reasonable rule of thumb is to use latest stable release version of library for the JRE version your software targets. Obviously you will find many exceptions from this rule, but in general stability of publicly available libraries usually increases with time.

Naturally API compatibility and versioning have to be considered when changing versions of dependencies.

Again most popular dependencies will have web pages where such information should be available.

If however you are using something a bit more obscure, you can discern which JRE were the classes within your dependency compiled for.

[Here is a great answer on how to find out class version][7]. You might need to unzip the JAR file first.

[3]: http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#incompatibilities "official list of incompatibilities" [4]: http://www.oracle.com/technetwork/java/javase/compatibility-137541.html#incompatibilities [5]: http://www.oracle.com/technetwork/java/javase/compatibility-137462.html#incompatibilities [6]: https://docs.oracle.com/javase/9/tools/jdeprscan.htm [7]: https://stackoverflow.com/a/1096159/76237

Solution 2 - Java

You would not encounter any problems - that's the magic of Java -it's backwards compatible.You can run almost all code from Java 1 on Java 8. There's no reason why Java 6 code won't run on a Java 8 Runtime.

What is interesting, is that for applications written in, let's say, Java 1.4, you even have speed increases when running them on later runtimes. This is because Java is constantly evolving, not just the language known as "Java", but also the JVM (Java virtual machine). I still have source code from more than 10 years ago that still work, as expected in the latest JVM.

If you want to target, let's say, a Java 5 VM, then you can do that with the Java 8 SDK tools. You can ultimately specify which target VM you wish to support, as long as you bear in mind that a version 5 VM might not support all the features a version 8 VM will.

I've just tested code I wrote in Java 5 against the new Java 8 runtime and everything works as expected, so, even though we have a more powerful language and runtime now, we can continue to use our investments of the past. Just that alone makes Java a great development choice for companies.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshawnView Question on Stackoverflow
Solution 1 - JavadiginoiseView Answer on Stackoverflow
Solution 2 - JavaEwaldView Answer on Stackoverflow