javac 源和目标选项用法 [英] javac source and target options usage

查看:39
本文介绍了javac 源和目标选项用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用 javac 选项时:
-source-target
编译器如何知道"要生成的代码?

When we use javac option:
-source and -target
how does the compiler "knows" the code to produce ?

我的意思是任何编译器如何知道任何以前的规范java release 或者我必须做一些设置才能提供它关于他们的一些信息?

I mean how does any compiler knows the specification of any previous java release or I have to do some setting in order to provide it with some information about them ?

推荐答案

javac 程序知道如何处理这些参数,而无需您提供任何额外信息.但是请注意,这里有一个陷阱:这些选项与源语法以及类文件形式和内容相关,但它们没有考虑到 Java 标准库.因此,他们将完全满足于编译依赖于标准库类或 Java 7 中新增的方法的代码,尽管源或目标 Java 版本被设置为更早的版本.原则上结果可以在较早的 Java 版本上运行,但在实践中,这将需要向后移植,至少部分地向后移植.

The javac program knows how to handle these arguments without you providing any extra information. Do note that there is a bit of a trap here, however: these options relate to the source syntax and the class file form and content, but they do not take into account the Java standard library. As a result, they will be perfectly content to compile your code that relies on standard library classes or methods that are new in (say) Java 7, despite the source or target Java version being set to something earlier. In principle the result could run on that earlier Java version, but in practice that would require the later standard library to be backported, at least in part.

如果您正在开发 Java 的特定最低版本,您应该使用那个版本.-source-target 选项在您或其他人以后想要使用更新的 javac 重建源代码时发挥作用,而不会丢失与早期目标 Java 的运行时兼容性.

If you are developing for a particular minimum version of Java, you should develop using that version. The -source and -target options options come into play if you or someone else later wants to rebuild the source using a later javac, without dropping runtime compatibility with the earlier target Java.

这篇关于javac 源和目标选项用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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