简体   繁体   中英

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

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

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

There are also differences in both the Java language and Java compilers' interpretation of the JLS between different versions of Java. But these differences typically lead to compilation errors, not to different code.


In reality, OpenJDK and Oracle JDK are pretty close. In fact, for matching versions I'd expect the bytecodes produced by the respective javac compilers to be virtually identical. 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? "

Erm ... no. There are differences in Java behaviour for different platforms; ie Java on Windows and Java on Linux behave differently in some respects. Some of these differences are directly attributable to the platforms themselves; eg pathname syntaxes and file locking are different on Windows and Linux. Others are due to issues with mapping from Java to the platforms' different native windowing system.

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.

Sitting with a jar file compiled with OracleJDK, that runs on that system. When I tried to run it on mine when I have OpenJDK installed, it refuses to run. And keeps giving me a missing class error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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