Is it possible to compile class files with the Java 7 SDK which can run on Java 6 JVMs?

t3chris picture t3chris · Apr 11, 2012 · Viewed 13k times · Source

Since the public Java 6 SE JRE is getting closer to it's EOL (Nov '12), I'm considering porting my projects from Java 6 to Java 7. This would'nt be a big deal, if Apple would provide a Java 7 JRE for Mac OS X. But since Apple isn't willing to do so, I still have to support users which only have a Java 6 JRE.

Is there a way to compile Java 6 compatible binaries (class files) with the Java 7 javac? Certainly I'm aware that I can't use the new Java 7 features when doing so.

Thanks in anticiption!

Answer

Stephen C picture Stephen C · Apr 11, 2012

It depends. If your program doesn't use the new Java 7 language extensions, then you can run the Java compiler with the -source 1.6 and -target 1.6 options. But if you use Java 7 language extensions then -source 1.6 will result in compilation errors.

Certainly I'm aware that I can't use the new Java 7 features when doing so.

That includes Java 7 language features ... and dependencies on Java 7 changes to the standard class library APIs. Also be aware that there are small number of behavioural differences (aka API bug fixes) that may cause code to run differently on Java 6 and Java 7. They should be described in the Java 6 to Java 7 transition document.


UPDATE - This probably no longer an issue for you anyway, because Oracle have released Java 7 for Mac OSX.