File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Features new in Java 7 and the fly likes Will Java 7 byte code run on older JVMs? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Will Java 7 byte code run on older JVMs?" Watch "Will Java 7 byte code run on older JVMs?" New topic
Author

Will Java 7 byte code run on older JVMs?

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

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

Joined: Mar 28, 2012
Posts: 859

You'll probably get the JRE vs JDK version problem at runtime.

WP
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4658
    
    5

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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Oct 13, 2005
Posts: 38859
    
  23
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Will Java 7 byte code run on older JVMs?