简体   繁体   English

java使用不同的JDK版本构建:相同的工件?

[英]java builds with different JDK versions : same artifacts?

here is my scenario: 这是我的情况:

  • workstation A : Sun JDK 1.6 工作站A:Sun JDK 1.6
  • workstation B : Sun JDK 1.5 工作站B:Sun JDK 1.5

I build on A and B with Ant 1.7 我使用Ant 1.7构建A和B

<javac srcdir="${foo}" destdir="${bar}" source="1.5" target="1.5">
  <include name="**/*.java"/>
  <exclude name="**/test/**/*.java"/>
</javac>

yet the .class files are different size on A & B, let alone CRC. 然而, .class文件在A和B上的大小是不同的,更不用说CRC了。 is it supposed to be this way? 应该是这样吗? or am I missing something obvious? 还是我缺少明显的东西?

Yes, it is supposed to be that way. 是的,应该是那样的。 Even when targeting the same VM, different compilers might emit different byte code. 即使针对相同的VM,不同的编译器也可能会发出不同的字节码。 As long as the resulting program behaves correctly, the JLS doesn't mandate precisely how source code is compiled to byte code. 只要生成的程序正常运行,JLS便不会严格要求如何将源代码编译为字节码。

While you might explicitly set source="1.5" and target="1.5" , this does not control which JDK version of the javac compiler you are running. 虽然你可能明确设置source="1.5"target="1.5" ,这控制你正在运行javac编译器的哪个JDK版本。

So, on the workstation with JAVA_HOME / PATH pointing to JDK 1.6, you have class files built on a 1.6 javac which should be compatible with a 1.5 JRE. 因此,在具有指向JDK 1.6的JAVA_HOME / PATH的工作站上,您具有基于1.6 Javac构建的类文件,该类文件应与1.5 JRE兼容。

However, the source="1.5" and target="1.5" only specify the compatibility of the syntax/language features; 但是, source="1.5"target="1.5"仅指定语法/语言功能的兼容性; if you are relying on some classes available only in the 1.6 JRE, then you will not get any compiler warning here when using source="1.5" and target="1.5" on a 1.6 javac. 如果您依赖于某些仅在1.6 JRE中可用的类,则在1.6 javac上使用source="1.5"target="1.5"在此不会收到任何编译器警告。

To address this last point, you need to tell the <javac> Ant task to use a different bootclasspath . 为了解决最后一点,您需要告诉<javac> Ant任务使用其他bootclasspath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM