Why is the JVM not platform independent, given that Java (the language) is platform independent?

The JVM executes Java code, but is written in platform specific languages such as C/C++/ASM etc. The JVM is not written in Java and hence cannot be platform independent.


Unless you have a CPU that can directly execute Java bytecode (there are such things) you need to be able to interact with the OS (for things like reading files, connecting to the network, displaying to the screen, etc...).

You can write a JVM in other languages (such as Java or JavaScript) but ultimately there needs to be something that can interact with the underlying OS.


I found that this was a great answer to the question:

JVM translates bytecode into machine language

Every Java program is compiled into an intermediate language called Java bytecode. The JVM is used to both translate the bytecode into the machine language for a particular computer, and actually execute the corresponding machine-language instructions as well. The JVM and bytecode combined give Java its status as a "portable" language.

Machine language is OS dependent

Given the previous information, it should be easier to deduce an answer to the question. Since the JVM must translate the bytecode into machine language, and since the machine language depends on the operating system being used, it is clear that the JVM is platform (operating system) dependent. This fact can be verified by trying to download the JVM – you will be given a list of JVM’s corresponding to different operating systems, and you will obviously pick whichever JVM is targeted for the operating system that you are running.

Quoted from Is the JVM Platform Dependent?

Tags:

Java

Jvm