Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...