1

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? "

Community
  • 1
  • 1
Aman Arora
  • 1,232
  • 1
  • 10
  • 26

4 Answers4

7

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks for the reply. [As mentioned here](http://stackoverflow.com/questions/9207288/differences-in-java-bytecode-produced-by-oracles-and-eclipses-compilers), the byte code itself is different.Then how it will run on different JVMs? – Aman Arora Nov 28 '13 at 14:28
  • @AmanArora Most likely the difference is not important for the running of the program. e.g. you can include debugging or not , use obfuscation, reorder the methods and the class will run the same. – Peter Lawrey Nov 28 '13 at 14:30
  • @AmanArora Don't forget the JVM doesn't actually run the byte code. It is translated into a data structure and possibly compiled to native code. All you have is a syntax checked binary format. The `javac` compiler doesn't even optimise 99% of cases. – Peter Lawrey Nov 28 '13 at 14:31
  • @AmanArora The OpenJDK is now declared to be the reference implementation. Anything which is different to it is a bug (according to OpenJDK anyway ;) – Peter Lawrey Nov 28 '13 at 14:33
  • Correct me if i am wrong please. What i have understood is : 'javac' is standard across all the JDKs and the byte code it produces should run on any JVM which follows [JVM specification](http://docs.oracle.com/javase/specs/jvms/se7/html/index.html) made by ORACLE – Aman Arora Nov 28 '13 at 14:36
  • 1
    @AmanArora Yes, but different versions of Java can produce subtly different byte code, however they should do the same thing. Note: Scala, groovy, JRuby, Jython etc all produce byte code and these should run on the major JVMs. Java is not the only language which runs on a JVM. – Peter Lawrey Nov 29 '13 at 00:39
1

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 (i.e. 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; i.e. Java on Windows and Java on Linux behave differently in some respects. Some of these differences are directly attributable to the platforms themselves; e.g. 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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I have shared the link where java code does not even compile with the same versions of OpenJDK and Oracle JDK. Here's [the link](http://mail.openjdk.java.net/pipermail/compiler-dev/2009-November/001645.html) – Aman Arora Nov 28 '13 at 14:43
  • Yes ... but that's not a code generation bug. It is a bug in the error checking that causes one compiler to accept something that the other (rightly) calls a compilation error. It is also comparing compiler behaviour across different **versions** of Java! – Stephen C Nov 28 '13 at 14:48
  • So OpenJDK has their own javac implementation, Oracle has its own. There can be cases where the bytecodes generated might not be cross-compatible? isn't it? – Aman Arora Nov 28 '13 at 14:56
  • It is possible, but highly unlikely. In fact, OpenJDK and Oracle JDK are produced by the same team of people. It is the same codebase!! – Stephen C Nov 28 '13 at 15:12
1

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.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • "Generating different bytecode is not necessarily generating bad bytecode". True ! But JVM has to translate that code to the native code and it can't translate any bytecode right ? – Aman Arora Nov 28 '13 at 14:39
0

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.

anon
  • 1