简体   繁体   English

使用OpenJDK编译的Java代码是否将始终在Oracle的Hotspot上运行,反之亦然?

[英]Will java code compiled using OpenJDK always run on Oracle's Hotspot or vice versa?

I came through this document where the same java code compiles in Oracle JDK but not on OpenJDK. 我浏览了该文档 ,在Oracle JDK中编译了相同的Java代码,而在OpenJDK上则没有。 Some references for the same problems are present here too on SO . 关于相同问题的一些参考文献也出现在SO上 Does it mean "javac" is vendor specific? 这是否意味着"javac"是特定于供应商的?

And if the answer is yes ? 如果答案是肯定的? then there is a possibility that they may produce different bytecode. 那么它们可能会产生不同的字节码。 Refer here . 请参考这里

So if the bytecode is different, How will Oracle's JVM handle bytecode generated by OpenJDK's javac? 因此,如果字节码不同,那么Oracle的JVM将如何处理OpenJDK的javac生成的字节码?

Is it safe to say: "Java is "Write Once and Run Anywhere, provided the javac compiler and JVM are from the same vendor? 可以肯定地说: “ Java是“一次编写即可在任何地方运行”,只要javac编译器和JVM来自同一供应商? "

The javac is not vendor specific, however different compilers can have different bugs and this can cause a difference. javac不是特定于供应商的,但是不同的编译器可能会有不同的错误,这可能会导致差异。

What makes much more difference is the built in libraries available, esp classes which are not intended to be used by developers. 更重要的是可用的内置库,特别是开发人员不打算使用的esp类。 eg sun.misc.Unsafe.copyMemory(5 args) didn't exist until Java 6 update 18 in Oracle JDK and is only available in the last update of OpenJDK. 例如,直到Oracle JDK中的Java 6更新18,sun.misc.Unsafe.copyMemory(5 args)才存在,并且仅在OpenJDK的最新更新中可用。 AFAIK, it is not available in IBM JVM. AFAIK,它在IBM JVM中不可用。

The Write Once, Run Anywhere means compile once, run anywhere. 一次写入,可在任何地方运行意味着一次编译,可在任何地方运行。 C++ for example can be written once and run anywhere provided you re-compile it for each system. 例如,只要为每个系统重新编译C ++,就可以编写一次,并且可以在任何地方运行。

Once you have compiled your Java code, it will run on any system which has the libraries you used. 编译Java代码后,它将在具有您使用的库的任何系统上运行。

Will java code compiled using OpenJDK always run on Oracle's Hotspot or vice versa? 使用OpenJDK编译的Java代码是否将始终在Oracle的Hotspot上运行,反之亦然?

If they are the same version, yes. 如果它们是相同版本,则是。

But if you compile on Java 7 and try to run on Java 6 or earlier, you will get problems (unless you use the -target switch appropriately). 但是,如果您在Java 7上编译并尝试在Java 6或更早版本上运行,则会遇到问题(除非您适当地使用-target开关)。

There are also differences in both the Java language and Java compilers' interpretation of the JLS between different versions of Java. 不同版本的Java之间,Java语言和Java编译器对JLS的解释也有所不同。 But these differences typically lead to compilation errors, not to different code. 但是这些差异通常会导致编译错误,而不是导致不同的代码。


In reality, OpenJDK and Oracle JDK are pretty close. 实际上,OpenJDK和Oracle JDK非常接近。 In fact, for matching versions I'd expect the bytecodes produced by the respective javac compilers to be virtually identical. 实际上,对于匹配的版本,我希望各个javac编译器生成的字节码实际上是相同的。 Compiler bug fixes made to one codebase are ported to the other as a matter of course, and code generation bugs in the bytecode compiler are pretty unusual. 当然,对一个代码库进行的编译器错误修复将移植到另一个代码库,并且字节码编译器中的代码生成错误非常少见。 Other differences in generated bytecodes (ie not due to bugs) are unlikely to impact on the behaviour of a properly written program. 生成的字节码中的其他差异(即不是由于错误)不太可能影响正确编写的程序的行为。


Is it safe to say: "Java is "Write Once and Run Anywhere, provided the javac compiler and JVM are from the same vendor? 可以肯定地说:“ Java是“一次编写即可在任何地方运行”,只要javac编译器和JVM来自同一供应商? "

Erm ... no. 嗯...不 There are differences in Java behaviour for different platforms; 不同平台的Java行为有所不同。 ie Java on Windows and Java on Linux behave differently in some respects. 即Windows上的Java和Linux上的Java在某些方面表现不同。 Some of these differences are directly attributable to the platforms themselves; 其中一些差异直接归因于平台本身。 eg pathname syntaxes and file locking are different on Windows and Linux. 例如,在Windows和Linux上,路径名语法和文件锁定是不同的。 Others are due to issues with mapping from Java to the platforms' different native windowing system. 其他原因则是由于从Java到平台的不同本机窗口系统的映射问题。

These differences are nothing to do with compilers or code generation. 这些差异与编译器或代码生成无关。

The best answer to your question would be "it depends." 最好的答案是“取决于情况”。 Generating different bytecode is not necessarily generating bad bytecode. 生成不同的字节码不一定会生成错误的字节码。 Bear in mind that the first document you reference is discussing OpenJDK 6 and Oracle JDK 6. Back then, OpenJDK and Oracle JDK were in fact often subtly incompatible because Oracle hadn't brought the two JDK projects together the way they did with JDK 7. Now they're almost identical code bases, but prior to 7 that wasn't the case. 请记住,您参考的第一个文档是关于OpenJDK 6和Oracle JDK 6的。那时,OpenJDK和Oracle JDK实际上常常是微妙的不兼容,因为Oracle并未像使用JDK 7那样将两个JDK项目整合在一起。现在它们几乎是相同的代码库,但在7之前不是这种情况。

Sitting with a jar file compiled with OracleJDK, that runs on that system. 坐在用OracleJDK编译的jar文件上,该文件在该系统上运行。 When I tried to run it on mine when I have OpenJDK installed, it refuses to run. 当我尝试在安装了OpenJDK的情况下在我的计算机上运行它时,它拒绝运行。 And keeps giving me a missing class error. 并且不断给我一个缺少的类错误。

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

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