Execution Process of Java Program / Phases in Compilation of Java Program
To understand the compilation execution process of java program you need to see the following figure.
Before understand the above figure lets we discuss the important task that are perform during the execution of a java program,
1) First you need a text editor (such as a notepad, etc.) to write a java program.
2) Once you write your first program then you need to compile that java program. For this purpose you need to install a java compiler to your machine.
3) Once your program is compiling and there is no compilation error then you need a java interpreter to execute the program and get the result.
In the above figure, first one can write a program named Hello.java using an editor (such as Notepad) then compile that Hello.java file using a compiler and the output of the compilation is Hello.class (.class file) then Hello.class is interpret by the interpreter.
In the above figure we use an editor to write the java program and save that program as Hello.java. Once the program is saved we compile the Hello.java using java compiler. Once Hello.java compilation is successful then we get Hello.class (output file after compilation). When we get the Hello.class then we interpret that .class file using java interpreter which gives the final result./
Note- Apart from all the above discussed tools another important tool is command prompt. This is used to execute java compiler (javac.exe) and java interpreter (java.exe). Once we run the java interpreter using command prompt then the result is also shown on the command prompt.
To understand better, let’s take an example program. This example program prints the “Hello World” on command prompt.
Steps to execute you first program-
1) First write the above code on a notepad file.
2) Save the file with the .java extension.
Note- In java we give the file name as the class name.
3) Now open the command prompt and first set the java (JDK) directory path. To set the path just type and run the following command-
set path= “c:\program files\java\jdk1.8\bin”;.;
Note- generally when you install the JDK then it is installed inside the program file (found under the C drive). I recommend before setting the path first check the JDK and bin folder location then use set path command.
4) Once your path is set you need to write the following command on CMD-
javac Hello.java
After typing the above command just press the enter button and the program is compiled.
5) If there is no error during the compilation then you get a .class file at the location where your java program is saved. Now we are ready for program execution. To execute the program you need the type the following command on CMD-
java Hello
After typing the above command just press the enter button and the program is executed and the output is shown on the command prompt.
Note- In every java program the main() method is the starting point for the JVM. It means JVM always starts from the main().
Now the one question that arises is that if a java program does not have the main method then it cannot be executed? I solved this issue here right now. Yes you can execute a java program without main but you need to write all the code inside a static block.
Every static block is run as soon as the class is loaded before the main() method. Generally every main method is declared with a static keyword so you do not need to call the main method with any object.
In Java every method runs when it is called by an object of the class.
Must Read – Java Interview Questions and answer You Need to Know