Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will Java 7 byte code run on older JVMs?

 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
News that Java 7 has significant improvements in compile time is very interesting to me.

But I dont' want to have to update all of the production servers, which have 1.6 JVMs.

Will the new compiler generate byte code that runs properly on older JVM?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll probably get the JRE vs JDK version problem at runtime.

WP
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can compile for an older JVM using -source and -target; that would be -source 1.6 -target 1.6 for Java 6. However, you will not be able to use any new language constructs like try-with-resources or switch statements with Strings. Likewise, if you choose 1.4 or before as the source and target, you won't be able to use generics.

There's one danger with this approach though; it doesn't prevent you from compiling code that uses classes, methods and fields that were added in Java 7, but that code will fail in a Java 6 JVM.
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about just compiling it with 1.7, no switches, and then running it on an older JVM.

I would expect that the string switch statement should work, as its just a bit of syntactic sugar and purely a compile time thing.

Obviously, if you use cool new features, like try-with-resources, they may need runtime support and that can't possibly work.
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say you're out of luck:

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

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.

Source.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:I was thinking about just compiling it with 1.7, no switches, and then running it on an older JVM. . . .
No. You will get the Exception about wrong version number in your .class files.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic