Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

This may be a stupid question, but 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?

like image 672
shawn Avatar asked Jun 05 '12 10:06

shawn


People also ask

Can we use different version of JDK and JRE?

The JDK and JRE versions can be different on the same computer. Multiple JDK and JRE versions are allowed on the same computer; it is better to find out which version is configured in the system classpath to run or compile the Java program.

Can program developed with Java 7 be run on Java 8?

Binary Compatibility Except for the noted incompatibilities, class files built with the Java SE 7 compiler will run correctly in Java SE 8. Class files built with the Java SE 8 compiler will not run on earlier releases of Java SE.

Is JDK 17 backwards compatible with JDK 11?

Short answer: Yes. That's the point. Your ancient byte code, even from Java 1, will run under Java 11.

Can Java run older versions?

The latest available version is compatible with older versions. However, some Java applications (or applets) can indicate that they are dependent on a particular version, and may not run if you do not have that version installed.


2 Answers

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:

  • in Java SE 9 since Java SE 8
  • in Java SE 8 since Java SE 7
  • in Java SE 7 since Java SE 6
  • in Java SE 6 since Java SE 5.0
  • in Java SE 5.0 since Java SE 1.4.2

Compatibility tool

Packaged with JDK 9, there is a tool called jdeprscan 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. You might need to unzip the JAR file first.

like image 77
diginoise Avatar answered Sep 27 '22 21:09

diginoise


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.

like image 37
Ewald Avatar answered Sep 27 '22 19:09

Ewald