在 Java 1.7 上编译 Java 1.5 仍然有效吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7856472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:42:31  来源:igfitidea点击:

Will compiling for Java 1.5 on Java 1.7 still work?

javajava-7java-5

提问by TheLQ

I've recently moved to Java 7 in one of my projects. I claim that it can run on Java 1.5 simply because there's nothing I depend on that is in Java 6 or 7. However when compiling today I noticed this:

我最近在我的一个项目中转向了 Java 7。我声称它可以在 Java 1.5 上运行仅仅是因为在 Java 6 或 7 中没有任何我依赖的东西。但是今天编译时我注意到了这一点:

bootstrap class path not set in conjunction with -source 1.5

Google has found little information on this warning. Does this mean that you can't compile to Java 1.5 from Java 1.7?

谷歌几乎没有找到有关此警告的信息。这是否意味着您不能从 Java 1.7 编译到 Java 1.5?

采纳答案by paulsm4

This Oracle blog explains the warning:

这个 Oracle 博客解释了警告:

http://blogs.oracle.com/darcy/entry/bootclasspath_older_source

http://blogs.oracle.com/darcy/entry/bootclasspath_older_source

The reason is, that if you fail to set rt.jar for the older platform, then:

原因是,如果您未能为旧平台设置 rt.jar,则:

If the second step is not taken, javac will dutifully use the old language rules combined with new libraries, which can result in class files that do not work on the older platform since references to non-existent methods can get included.

如果不采取第二步,javac 将尽职尽责地使用旧的语言规则与新的库相结合,这可能导致类文件在旧平台上不起作用,因为可能包含对不存在的方法的引用。

回答by Stephen C

Does this mean that you can't compile to Java 1.5 from Java 1.7?

这是否意味着您不能从 Java 1.7 编译到 Java 1.5?

No it doesn't. It means that there is a right way and a wrong way to do this ... and you are doing it the wrong way.

不,它没有。这意味着有一种正确的方法和一种错误的方法来做到这一点……而你却以错误的方式去做。

The right way to compile for the Java 1.5 on a Java 1.7 JDK is:

在 Java 1.7 JDK 上为 Java 1.5 编译的正确方法是:

  • Get hold of a copy of the "rt.jar" from Java 1.5 and put it on the compilation bootclasspath.

  • Compile with -source 1.5 and -target 1.5.

  • 从 Java 1.5 中获取“rt.jar”的副本并将其放在编译引导类路径中。

  • 使用 -source 1.5 和 -target 1.5 编译。

The warning message is telling you that you haven't done the first of these.

警告消息告诉您,您还没有完成其中的第一个。



The way that you are building right now is implicitly using the 1.7 version of "rt.jar" for the Java runtime APIs. This maywork! (Indeed, it shouldwork assuming that you've made no changes to the code since it last built on 1.5.) However, there is a risk that you may accidentally introduce dependencies on classes or methods added in Java 1.6 or 1.7. That would result in runtime errors when you try to run your application on Java 1.5.

您现在正在构建的方式是隐式使用 1.7 版本的“rt.jar”作为 Java 运行时 API。这可能有效!(实际上,假设自上次构建在 1.5 上以来您没有对代码进行任何更改,它应该可以工作。)但是,存在风险,您可能会不小心引入对 Java 1.6 或 1.7 中添加的类或方法的依赖关系。当您尝试在 Java 1.5 上运行应用程序时,这会导致运行时错误。

回答by bmargulies

  1. You better be setting -source and -target 1.5.

  2. To be really sure that you aren't accidentally incorporating dependencies on newer classes, methods, or fields, use the maven-animal-sniffer plugin or something like it.

  1. 你最好设置 -source 和 -target 1.5。

  2. 要真正确保您不会意外地将依赖项合并到较新的类、方法或字段,请使用 maven-animal-sniffer 插件或类似的插件。

回答by gregturn

--source 1.5 will make sure the source files comply with Java 5 conventions. --target 1.5 will make sure the generated class files comply with Java 5 conventions. Neither of these will protect you from using Java 6 or 7 library methods. You must either compile against the appropriate rt.jar using --bootclasspath, or use something like the animal-sniffer-plugin (if you are using maven) which will inspect everything's type signature, and compare with published profiles.

--source 1.5 将确保源文件符合 Java 5 约定。--target 1.5 将确保生成的类文件符合 Java 5 约定。这些都不会保护您免于使用 Java 6 或 7 库方法。您必须使用 --bootclasspath 对适当的 rt.jar 进行编译,或者使用类似动物嗅探器插件的东西(如果您使用的是 maven),它将检查所有内容的类型签名,并与已发布的配置文件进行比较。

With the animal-sniffer-plugin, you may be in for a treat, because you can bump into 3rd party libraries that use Java 6 APIs, which may cause your build process to fail given you are pursing Java 5.

使用animal-sniffer-plugin,您可能会大吃一惊,因为您可能会遇到使用Java 6 API 的第3 方库,这可能会导致您的构建过程失败,因为您正在追求Java 5。