[Solved] How the .class file from java can be ran in almost all environments [closed]


I will try to explain this in as simple terms as possible.

You write your Java code and save it in a .java file. The .java file is then turned into a .class file by the Java compiler.

Now what does this .class file contain? Does it contain machine code that a processor can directly run? No. If it really did Java wouldn’t have been cross-platform. .class files contain Java bytecode.

No processor can directly run Java bytecode. People have to install something called a “Java Runtime Environment” (JRE). The JRE can read bytecode! The JRE interprets the bytecode in the .class file and turns them into processor instructions.

Since there are different processor architectures (different kinds of processors) and different OSs, there needs to be different JREs. There is one JRE for Windows 32 bit, one for Windows 64 bit, one for Mac etc.

Here’s an analogy:

You wrote something in English and you want a Japanese speaker, a Chinese Speaker, and a Korean speaker to understand what you wrote. How do you do this. You hire a translator for Japanese, Chinese and Korean respectively! So that the same English text can be understood by all 3 people!

solved How the .class file from java can be ran in almost all environments [closed]