java 7语言向后兼容性 [英] java 7 language backwards compatibility

查看:164
本文介绍了java 7语言向后兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短问题:如果我使用以前在Java 6中不可用的相对次要Java 7语言功能,例如try-muticatch块...这是否意味着我的程序将无法在具有JRE 6或JRE的计算机上运行5编译后安装为?如果这是正确的,是否可以快速生成JRE6 .jar可执行文件而无需更改Java 7源代码(顺便提一下,它将使用的唯一Java 7功能是try-multicatch块)?

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

推荐答案

你是对的。 Multi-catch是一种Java 7语言特性,无法将其编译为Java 6(或更早版本)JVM兼容字节码。

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.

使用Java 7编译器,以下允许您编译Java 6兼容字节码:

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

javac -source 1.6 -target 1.6 MyJavaFile.java

当您尝试编译Java 7语言功能(例如,多次捕获)时,您将获得:

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

(有关该<$的更多信息c $ c>警告表示,请参阅:
https://blogs.oracle.com/darcy/entry/bootclasspath_older_source - 这与此讨论没有关系)

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

如果您将 -source 标志更改为 1.7 您将收到:

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


源版本1.7需要目标版本1.7

source release 1.7 requires target release 1.7

因为......你无法编译Java 7来源(例如具有Java 7功能的源代码)与Java 6兼容的字节码。

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

如果用Java 7编译它(没有 -source -target flag)你将得到Java 7字节码,它不能在< Java 7 JVM。如果你试图这样做,你会收到一个错误,告诉你版本不匹配:

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

线程中的异常mainjava.lang.UnsupportedClassVersionError:
net / mostlyharmless / multicatch / App:不支持的major.minor版本
51.0

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

这篇关于java 7语言向后兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆