I came through this document where the same java code compiles in Oracle JDK but not on OpenJDK. Some references for the same problems are present here too on SO.
Does it mean "javac" is vendor specific?
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?
Is it safe to say: "Java is "Write Once and Run Anywhere, provided the javac compiler and JVM are from the same vendor? "
The javac is not vendor specific, however different compilers can have different bugs and this can cause a difference.
What makes much more difference is the built in libraries available, esp classes which are not intended to be used by developers. e.g. 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. AFAIK, it is not available in 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.
Once you have compiled your Java code, it will run on any system which has the libraries you used.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With