我可以使用带有较新 JDK 的 ant 为较旧的 Java 版本编译 java,以便在编译使用较新 API 的代码时生成输出吗? [英] Can i compile java for an older Java version with ant with a newer JDK so it generates output when it compiles code that uses the newer API?

查看:25
本文介绍了我可以使用带有较新 JDK 的 ant 为较旧的 Java 版本编译 java,以便在编译使用较新 API 的代码时生成输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在我的系统上安装 JDK 8,并让 ant javac 编译操作为 java 6 环境创建工作类文件.

I would like to only have JDK 8 installed on my system, and have the ant javac compile action create working classfiles for a java 6 environment.

如果我在语法上只使用符合 java 6 的代码,这种类型的工作,但我的代码可以访问 java 8 API 的方法/类,并且根据 ant 的编译操作仍然符合 java 6.

This sort of works if I syntactically only use java 6 compliant code, but my code can access methods/classes of the java 8 API and still be java 6 compliant according to ant's compile action.

当使用javac"任务属性source"和target"设置为java 6时,情况也是如此.我知道这会产生以下警告:[javac]警告:[options] bootstrap class path not与 -source 1.6 一起设置.但这不是帮助我检测代码是否实际使用新引入的 API 元素的警告类型.

This is even the case when using the "javac" task attributes "source" and "target" set to java 6. I am aware that this generates the following warning: [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.6. But this is not the type of warning that helps me detect if my code actually uses newly introduced API elements.

当正在编译的代码尝试使用 java 8 API 引入的方法/类时,我需要编译操作以某种方式做一些事情.

I need the compile action to somehow do something when the code that's being compiled tries to make use of java 8 API introduced methods/classes.

举个例子:如果使用 JDK 8 编译,我可以使用 Java 6 兼容代码访问 java.lang.reflect.Constructor.getParameters(),这是从 Java 8 引入的父类 Executable 继承的方法.当代码在 java 6 执行环境中运行时,执行该语句将导致抛出 NoSuchMethodException,这是不可预见的,因此也无法处理.我需要我的 ant 编译操作生成某种不同类型的输出(暂停或警告或其他),以便我可以自动执行某些操作.

As an example: with java 6 compliant code I can access java.lang.reflect.Constructor.getParameters() if copmiled using JDK 8, which is a method inherited from a Java 8 introduced parent class Executable. When the code runs in a java 6 exeuction environment, executing that statement will result in a thrown NoSuchMethodException, unforseen and thusly unhandled as well. I need my ant compile action generate some different kind of output (a halt or a warning or something else) so that I can automate something on that.

在这种情况下,有没有蚂蚁相关的解决方案?

Is there an ant-related solution for me in this scenario?

推荐答案

你写的:

我知道这会产生以下警告:[javac] 警告:[options] 引导类路径未与 -source 1.6 一起设置.

I am aware that this generates the following warning: [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.6.

此警告为我们指明了正确的解决方案.除了 -source-target 选项,使用 javac-bootclasspath 选项指定引导程序指向 1.6rt.jar 的类路径.您的计算机上不必安装完整的 JDK 1.6;包含 API 类的 jar 就足够了.

This warning points us toward the correct solution. In addition to the -source and -target options, use javac's -bootclasspath option to specify a bootstrap class path pointing to the rt.jar of 1.6. You do not have to have a complete JDK 1.6 on you computer; that jar, which contains the API classes, is sufficient.

Oracle 有进一步的说明和交叉选项-在其网站上编译.

请注意,此问题已由 JEP 247 解决,有望将其纳入 Java9 发布.然后,将有一个 -release 编译器选项,将 -source-target-bootclasspath 与一个JDK 本身附带的适当文件.因此,您不再需要从旧版 JDK 获取 rt.jar.

Note that this issue is addressed by JEP 247, which hopefully will make it into the Java 9 release. Then, there will be a -release compiler option, combining -source, -target and -bootclasspath with an appropriate file shipping with the JDK itself. So you don’t need to get that rt.jar from an older JDK anymore.

这篇关于我可以使用带有较新 JDK 的 ant 为较旧的 Java 版本编译 java,以便在编译使用较新 API 的代码时生成输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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