Video in progress
How does the "Write Once, Run Anywhere" (WORA) idea work technically in Java?
The code is compiled to platform-independent bytecode, and each OS and processor has its own JVM. The same .class file runs on Linux and on Windows, on that platform's JVM.
📖 Answer
In C, a program is compiled straight to x86 or ARM machine code and has to be recompiled on another platform. Java has two stages:
javac: from.javato.class(bytecode). This result is identical on every platform.- The JVM: at runtime it interprets the bytecode or turns it into the native code of that machine through the JIT.
That is, the platform-dependent part is hidden inside the JVM itself. You copy an app.jar built on a Mac to a Linux server and it works.
There are limits, too:
- native libraries (JNI,
.so/.dll) are platform-dependent; - file paths (
\and/), line endings and encodings can differ: usePath.of(...)and an explicitUTF-8; - a class built with a newer Java does not run on an older JVM (
UnsupportedClassVersionError).
Sign up to mark studied questions and save your progress.