Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I compiled a Java file with the newest JDK, would an older JVM be able to run the .class files?

Tags:

java

jvm

Does the bytecode depend on the version of Java it was created with?

like image 417
m4design Avatar asked Oct 31 '10 05:10

m4design


People also ask

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

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.

How do I compile older versions of Java?

Compiling for an older execution platform If you need to compile Java to run on an older Java platforms, the simplest approach is to install a JDK for the oldest version you need to support, and use that JDK's compiler in your builds. You can also compile with a newer Java compiler, but there are complicated.

How can I tell what version of Java a class was compiled with?

In Java, we can use javap -verbose className to print out the Java class file information; the output includes the major version number. Or we can use the javap -verbose JavaClassName | grep major to display only the major version of the Java class file.


2 Answers

If I compiled a java file in the newest JDK, would an older JVM be able to run the .class files?

That depends on three things:

  • The actual Java versions you are talking about. For instance, a 1.4.0 JVM can run code compiled by a 1.4.2 compiler, but a 1.3.x JVM cannot1.

  • The compilation flags used. There is a -target compiler flag that tells it to generate code that will run on an older (target) JVM. And the -source compiler flag tells it to only accept the older JVM's language features. (This approach won't always work, depending on the Java language features used by your code. But if the code compiles it should work.)

  • The library classes that the class file uses. If it uses library classes that don't exist in the older class libraries, then it won't run ... unless you can include a JAR that back-ports the classes2. You can avoid this problem by using the -bootclasspath option to compile your code against the APIs of the older version of Java.

Does the bytecode depend on the version of the java it was created with?

Yes, modulo the points above.


1 - The Java 8 JVMS states this: "Oracle's Java Virtual Machine implementation in JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. JDK releases 1.1.* support class file format versions in the range 45.0 through 45.65535 inclusive. For k ≥ 2, JDK release 1.k supports class file format versions in the range 45.0 through 44+k.0 inclusive."

2 - A backport could be problematic too. For example: 1) Things which depend on native code support would most likely require you to implement that native code support. 2) You would most likely need to put any back-port JAR file onto the bootclasspath when you run the code on the older JVM.

like image 53
Stephen C Avatar answered Oct 20 '22 11:10

Stephen C


Does the bytecode depend on the version of the java it was created with?

Normally yes. But by using the -source, -target and -bootclasspath options, a 1.7+ compiler can be used to create binaries that are compatible with Java 1.1

like image 43
Andrew Thompson Avatar answered Oct 20 '22 12:10

Andrew Thompson