Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 7 language backwards compatibility

Brief question: If I use relatively "minor" Java 7 language features previously unavailable in Java 6, such as the try-muticatch block... does this imply that my program won't run in machines with JRE 6 or JRE 5 installed after being compiled as is? If that's correct, is there a quick way to produce a JRE6 .jar executable without changing a Java 7 source code (which, by the way, the only Java 7 feature it would use is the try-multicatch block)?

like image 934
deinocheirus Avatar asked Jun 18 '13 11:06

deinocheirus


2 Answers

You are correct. Multi-catch is a Java 7 language feature and there is no way to compile it to Java 6 (or earlier) JVM compatible bytecode.

Using a Java 7 compiler, the following allows you to compile Java 6 compatible bytecode:

javac -source 1.6 -target 1.6 MyJavaFile.java

When you attempt to compile a Java 7 language feature (multi-catch, for example) you will get:

roach$ javac -source 1.6 -target 1.6 test.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
test.java:9: error: multi-catch statement is not supported in -source 1.6
    } catch (NullPointerException | BufferOverflowException ex) {}
                                  ^
  (use -source 7 or higher to enable multi-catch statement)
1 error
1 warning

(For more about what that warning means, see: https://blogs.oracle.com/darcy/entry/bootclasspath_older_source - it's not relavent to this discussion)

If you change the -source flag to 1.7 you will receive:

source release 1.7 requires target release 1.7

Because ... you can't compile Java 7 source (e.g. source that has Java 7 features) to Java 6 compatible bytecode.

If you compile it with Java 7 (with no -source or -target flag) you will get Java 7 bytecode which can not be run on a < Java 7 JVM. And if you try to do so you will receive an error telling you the versions don't match:

roach$ /Library/Java/Home/bin/java net.mostlyharmless.multicatch.App
Exception in thread "main" java.lang.UnsupportedClassVersionError: net/mostlyharmless/multicatch/App : Unsupported major.minor version 51.0

like image 52
Brian Roach Avatar answered Nov 14 '22 02:11

Brian Roach


You cant compile source with Java 7 features into Java 6 .class because this

javac -source 1.7 -target 1.6 Test.java

produces source release 1.7 requires target release 1.7 error. This is because some of the 1.7 features can work only with Java 7 classes. Eg try-with-resources uses Throwable.addSuppressed method available only since 1.7

like image 44
Evgeniy Dorofeev Avatar answered Nov 14 '22 02:11

Evgeniy Dorofeev