Will compiling for Java 1.5 on Java 1.7 still work?

你说的曾经没有我的故事 提交于 2019-12-18 15:02:32

问题


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?


回答1:


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:

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.




回答2:


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

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.




回答3:


  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.




回答4:


--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.



来源:https://stackoverflow.com/questions/7856472/will-compiling-for-java-1-5-on-java-1-7-still-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!