tags:

views:

109

answers:

7

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

I hope the question is clear.

Thanks.

+2  A: 

JVM bytecode is forward compatible between major JVM version, but not backward compatible. However, for the best information you will have to read the JVM release notes because they typically indicate how backward compatible the bytecode is.

Edit clarification since this caused discussion in the comments

JVM bytecode is forward compatible, such that bytecode from one JVM is compatible with with later JVM releases. For example, you can take bytecode from the 1.4 JVM and run it in Java 5 or Java 6 JVM (aside from any sort of regression issues as pointed out by Andrew).

JVM bytecode is not backward compatible between JVMs, such that bytecode from a JVM is not guaranteed to work in a previous release of the JVM, as would be the case if you were attempting to run code compiled for Java 6 in a 1.4.2 JVM.

birryree
@birryree: New versions are backward compatible. Old versions are not forward compatible. This will make more sense, what say you?
Adeel Ansari
@Adeel: I'd say it is your comment that is nonsensical. An applet created in Java 1.1 should run just fine in Java 1.6 barring regression bugs.
Andrew Thompson
@Andrew: I am saying exactly the same thing. Did I put it somehow different? New versions are backward compatible, means 1.1 applet should run just fine in 1.6.
Adeel Ansari
@Adeel "1.1 applet should run just fine in 1.6" is something I believe we all agree on (with minor caveats already mentioned), but birryree's words would have meant that to my eyes, whereas your statement meant the opposite. At least as far as I understand it! How about we stick to "1.1 applet should run just fine in 1.6" to avoid confusion? ;)
Andrew Thompson
@Andrew: You started me to doubt my own sentence. Yes, we should stick to your suggestion. The problem here is, I am not native English speaker, so its likely that you are right. :)
Adeel Ansari
+2  A: 

First and foremost all java files have a version byte in the class header. Older jvms won't load classes with newer versions, regardless of what features they have.

MeBigFatGuy
+1  A: 

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

Yes.

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

No. But the opposite will work, most likely. You might like see this interesting thread, it talks about backporting Java.

Adeel Ansari
+4  A: 

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.6+ compiler can be used to create binaries that are compatible with Java 1.1

Andrew Thompson
+6  A: 

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.5 JVM can run code compiled by a 1.6 compiler, but a 1.4.x JVM cannot. (The Java class file version number is only incremented when the classfile format changes in a way that is not backwards compatible. This doesn't happen with all releases.)

  • The compilation flags used. There is a -target compiler flag that tells it to generate code that will run on an older (target) JVM. (This approach won't always work, depending on the Java language features used. 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 classes.

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

Yes, modulo the points above.

Stephen C