Because it have JVM.
Firstly, java source will be compile into bytecode by Javac compiler. Bytecode is formatted as .class files
After that JVM is responsible for interpreting bytecode into machine code and program can execute with machine code
This process don’t depend on platform
-> Java is platform-independent