我可以使用较新的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?

查看:490
本文介绍了我可以使用较新的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类路径未与-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.

作为一个例子:使用java 6兼容代码我可以访问java.lang.reflect.Constructor。 getParameters()如果使用JDK 8进行copmiled,JDK 8是从Java 8引入的父类Executable继承的方法。当代码在java 6 exeuction环境中运行时,执行该语句将导致抛出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] bootstrap class path not not与-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 选项,用于指定指向 rt.jar <$ c $的引导类路径C> 1.6 。您不必在计算机上安装完整的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 解决了这个问题,希望它能够进入Java 9版本。然后,会有一个 -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天全站免登陆