java 7 language backwards compatibility

痴心易碎 提交于 2019-11-29 13:57:40

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!