Search code examples
java

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


Would I encounter any problems running Java programs and associated libraries compiled in earlier version of Java when using it later version of Java?

I'm compiling using 1.7 whereas some libraries are compiled using 1.6 and running the entire program in a 1.7 JRE?


Solution

  • *** UPDATE ***

    Since around 2020, the below answer is sadly no longer accurate. So much has changed in Java over the previous decade and compatibility was sacrificed to move things forward.

    While it remains true that most of your code and knowledge transfers to newer versions, there are many exceptions these days, though, in my experience, migrations can sometimes be quite easy.

    *** UPDATE ENDS ***

    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.