简体   繁体   中英

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

这可能是一个愚蠢的问题,但是在运行用Java 1.6 1.7编译的Java程序和相关库(我使用1.7进行编译,而某些库是使用1.6进行编译)并在1.7 JRE中运行整个程序时,我会遇到任何问题?

As answered already you are mostly safe and most products and 3 rd 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 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM