A java program is run successfully after a source code that we write goes through all the four phases below :
Step1 : Source code- Its the code or the program you create either rite or wrong :P. Here your java program is run as filename.java. Note: The filename should be the main class name!
Step2 : Compiler - Run your document source code through the compiler, compiler checks for errors and it wont let you compile until everything in your source code is correct.
Step3 : Output - The compiler creates a new document, coded into java bytecode. The java bytecode is platform independent.. Its here the filename.java is converted to filename.class file.
Step4 : Virtual Machine - JVM(Java Virtual Machine) The virtual machine reads and runs the bytecode.
Java is Platform independent, which means it can run on any platform, this is because all your gadgets which run on java program is not a physical java machine, instead it has a virtual java machine running inside their electronic gadgets, this is called JVM, which is capable of running all your Java Bytecodes to make your program run successfully to produce the desired output !