File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes platform-independent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "platform-independent" Watch "platform-independent" New topic


Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi All

Plz clear this.

Why is the compiler platform-independent, while JVM is platform-dependent?

Thanx in advance

Shrawan Bhageria
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Hi Shrawan,

A Java compiler performs the job of compiling source (*.java) files to bytecode (*.class). This bytecode is further interpreted by JVM to produce output.

To enrich Java with the feature of write once, run anywhere, it is important that the bytecode generated from a particular source file must be exactly similar in every operating system (so that, you can write your code in Linux, compile it and send your class file to your friend who might want to run it on Windows). Thus the job of a compiler is independent of any operating system. It just needs to convert the source code (which is of a defined syntax) to bytecode (this is also in a defined format, independent of the underlying OS).

For JVM, however, the matter is not so simple. Consider two JVMs, one running on Windows and another one running on Linux. Both have been given similar class file (naturally, resulting from same source code). As per JVM specification, both JVMs must produce exact same output on their respective operating systems. A JVM itself is just a program which executes on top of operating system. Thus, it is dependent on the same for almost every task (memory allocation, resource access, input/output etc.). In other words, a JVM has to know how to work with the underlying operating system, leveraging its supporting features/APIs. This is why JVM is always platform-specific.


- Aditya
Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Thanks Aditya.

Shrawan Bhageria
I agree. Here's the link:
subject: platform-independent
jQuery in Action, 3rd edition