8. Steps To Run A Java Program

Woah, wait a second..

There are five phases: edit, compile, load, verify and execute. Phase 1 consists of editing a file with an editor program, normally known simply as an editor. Using the editor, you type a Java program (typically referred to as source code), make any necessary corrections and save it on a secondary storage device, such as your hard drive. Java source code files are given a name ending with the .java extension, indicating that the file contains Java source code. In Phase 2, you use the command javac (the Java compiler) to compile a program. For example you write 'java HelloWorld.java' inside the command prompt if you are on Windows and inside the Terminal if you are on OSX. If the program compiles, the compiler produces a .class file called HelloWorld.class that contains the compiled version. IDEs typically provide a menu item, such as Build or Make, that invokes the javac command for you. If the compiler detects errors, you’ll need to go back to Phase 1 and correct them. The Java compiler translates Java source code into bytecodes that represent the tasks to execute in the execution phase. A virtual machine (VM) is a software application that simulates a computer but hides the underlying operating system and hardware from the programs that interact with it. If the same VM is implemented on many computer platforms, applications written for that type of VM can be used on all those platforms. The JVM is one of the most widely used virtual machines. Bytecode instructions are platform independent. So, Java’s bytecodes are portable, without recompiling the source code, the same bytecode instructions can execute on any platform containing a JVM that understands the version of Java in which the bytecodes were compiled. The JVM is invoked by the java command. For example, to execute a Java application called HelloWorld, you’d type the command 'java HelloWorld' in a command window to invoke the JVM, which would then initiate the steps necessary to execute the application. In Phase 3, the JVM places the program in memory to execute it, this is known as loading.The JVM’s class loader takes the .class files containing the program’s byte- codes and transfers them to primary memory. It also loads any of the .class files provided by Java that your program uses. The .class files can be loaded from a disk on your system or over a network. In Phase 4, the bytecode verifier examines their bytecodes to en- sure that they’re valid and do not violate Java’s security restrictions. In Phase 5, the JVM executes the program’s bytecodes, thus performing the actions specified by the program.