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.
Regards,
- Aditya