Thursday, April 17, 2008

Is JVM platform independent or not?


Though java is platform independent why is that I need to download a different JDK based on my Operating System was the question I was asking myself, for some time. The major difference in these different JDK seems to be the JVM. JVM interprets the bytecode into actions or Operating System calls that performs the required functions. It was said to me that these Operating System calls are specific to those Operating Systems and hence the JVM is not platform independent. I searched my web on this, just to get confusing answers.

These are some posts from the SDN forums:

MLRon writes:

JVM is not platform independent. The code you write to run on a JVM should be (unless you interface to native methods). If Microsoft comes up with an operating system that isn't compatible with Windows, then they (or someone) will have to make a new JVM for that new operating system. The Windows JVM is different from the Unix JVM or the Linux JVM. But, your Java code can run on all three without being changed or recompiled (again, unless you interface to native methods, or use other platform-specific stuff).


Herko_ter_Horst writes:

Java "the language" is platform-independent. To make it so, there is a platform-specific JRE that knows how to run the platform-independent Java code on a specific platform.

The JVM could be called platform-independent because it produces the same results running the same Java code on different platform(*). It is however (at least partially) implemented as a platform-dependent executable.

(*) assuming you don't try to use such things as Windows-specific file-names on Linux and all that

Jverd writes:

However, there are two big things here, both of which others have been trying to tell you:

1) With Java, you don't have to recompile. You pick up the .class file, move it over, without the source code, and it runs fine (provided you have a compatible VM on your target platform. The VM is not platform independent, but the Java source code is).

Arul writes:

[my comments: He says this replying to why JVM is not platform independent] Because the Java Virtual Machine is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris , Linux, or MacOS. Some virtual machines, such as the Java HotSpot Virtual Machine , perform additional steps at runtime to give your application a performance boost. This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently-used sections of your code. [my comments: these exact words are used in Sun's Java Tutorial]

a.java --> compiler --> a.class [class files do not have code native to your processor, it instead contain byte codes]

a.class --> Interpreter JVM for solaris --> Solaris
a.class --> Interpreter JVM for Windows --> Windows and son

Now to other sources:

Webopedia:

...JVM is a platform-independent execution environment that converts Java bytecode into machine language and executes it....


Wikipedia:

....Although Java programs are Platform Independent, the code of the Java Virtual Machine (JVM) that execute these programs are not. Every supported operating system has its own JVM.

The Java Virtual Machine Specification, Second Edition:

Sun's current Java virtual machine implementations, components of its JavaTM 2 SDK and JavaTM 2 Runtime Environment products, emulate the Java virtual machine on Win32 and Solaris hosts in much more sophisticated ways. However, the Java virtual machine does not assume any particular implementation technology, host hardware, or host operating system. It is not inherently interpreted, but can just as well be implemented by compiling its instruction set to that of a silicon CPU. It may also be implemented in microcode or directly in silicon.

So what does it mean, is JVM platform independent or not?

0 comments: