2

I just wanted to know if the bytecode generated in one version of java will work on other versions of java

1
  • you can google "java binary compatibility" Commented Mar 12, 2013 at 1:32

2 Answers 2

10

In general, bytecode will run without modification on a newer version of Java. It will not run on an older version unless you compile it with special arguments (javac -target) and take extreme care not to use the new library classes.

Sign up to request clarification or add additional context in comments.

2 Comments

You will also need to use -source in order to use -target. In some cases, source which compiles on earlier versions will pick up different overloaded methods, so "extreme care" isn't quite up to it. Netter to use -bootclasspath to point to an earlier rt.zip, and for extension libraries there is something like -Dext.dirs.
But really, at that point, it's probably best to just have parallel JDK installs...
4

Binary Compatibility

The class file version for Java SE 7 is 51, as per the JVM Specification, because of the invokedynamic byte code introduced by JSR 292. Version 51 class files produced by the Java SE 7 compiler cannot be used in Java SE 6.

Java SE 7 is binary-compatible with Java SE 6 except for the incompatibilities . Except for the noted incompatibilities, class files built with the Java SE 6 compiler will run correctly in Java SE 7.

Friends Words ...
The compiler is not backwards compatible because bytecode generated with Java7 JDK won't run in Java 1.6 jvm (unless compiled with the -target 1.6 flag). But the JVM is backwards compatible, as it can run older bytecodes.

So they chose to consider the compatibility from the point of view of javac (as it is the part specific to the JDK), meaning that the bytecode generated can be run in future releases of the jvm (that is more related to the JRE, but also bundled in the JDK).

In brief, we can say:

JDK's are (usually) forward compatible.
JRE's are (usually) backward compatible.

Java Says

Cross-Compilation Options

By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspath and -extdirs when cross-compiling; see Cross-Compilation Example below.

-target version
    Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).

    The default for -target depends on the value of -source:

        If -source is not specified, the value of -target is 1.7
        If -source is 1.2, the value of -target is 1.4
        If -source is 1.3, the value of -target is 1.4
        If -source is 1.5, the value of -target is 1.7
        If -source is 1.6, the value of -target is 1.7
        For all other values of -source, the value of -target is the value of -source.

-bootclasspath bootclasspath
    Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives. 

For More about Cross-Compilation look at http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-options

Better than me at http://www.oracle.com/technetwork/java/javase/compatibility-417013.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.