在 Java 1.7 上编译 Java 1.5 是否仍然有效?
我最近在我的一个项目中迁移到了 Java 7。我声称它可以在 Java 1.5 上运行,只是因为我不依赖于 Java 6 或 7 中的任何东西。但是,今天编译时我注意到这一点:
bootstrap class path not set in conjunction with -source 1.5
Google 几乎没有找到有关此警告的信息。这是否意味着不能从 Java 1.7 编译到 Java 1.5?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此 Oracle 博客解释了该警告:
http://blogs.oracle.com/darcy/entry/bootclasspath_older_source< /a>
原因是,如果您未能为旧平台设置 rt.jar,则:
This Oracle blog explains the warning:
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:
不,没有。这意味着做这件事有正确的方法和错误的方法……而你正在以错误的方式做这件事。
在 Java 1.7 JDK 上编译 Java 1.5 的正确方法是:
从 Java 1.5 获取“rt.jar”的副本,并将其放在编译引导类路径中。
使用 -source 1.5 和 -target 1.5 进行编译。
警告消息告诉您您还没有完成其中的第一个操作。
您现在构建的方式是隐式使用 1.7 版本的“rt.jar”作为 Java 运行时 API。这可能有效! (事实上,假设您自上次在 1.5 上构建以来没有对代码进行任何更改,它应该可以工作。)但是,存在您可能会意外引入对添加的类或方法的依赖项的风险。 Java 1.6 或 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:
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.
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 may work! (Indeed, it should work 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.
您最好设置 -source 和 -target 1.5。
要真正确保您不会意外地合并对较新的类、方法或字段的依赖项,请使用 maven-animal-sniffer 插件或类似的东西。
You better be setting -source and -target 1.5.
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.
--source 1.5 将确保源文件符合 Java 5 约定。 --target 1.5 将确保生成的类文件符合 Java 5 约定。这些都不能保护您使用 Java 6 或 7 库方法。您必须使用--bootclasspath针对适当的rt.jar进行编译,或者使用类似animal-sniffer-plugin(如果您使用maven)的东西,它将检查所有内容的类型签名,并与已发布的配置文件进行比较。
有了animal-sniffer-plugin,您可能会很高兴,因为您可以遇到使用 Java 6 API 的第三方库,这可能会导致您的构建过程失败,因为您正在追求 Java 5。
--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.
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.