Java compilation and Execution Flow
Updated: Feb 28
Understanding JDK, JRE, and the JVM
Have you ever wonder how we are getting output in console when we run java program in any IDE like intelliJ or Eclipse. Lets see how the execution flow happens
To run any java program in your system you need 2 things to be installed in your system
JDK — Java Development Kit
IDE — Integrated Development Kit
What is JDK? — JDK is a combination of Java Runtime Environment(JRE), java Compiler (Javac) and Development tools
JRE consists of Java Virtual Machine(JVM) and other Library classes.
What is IDE? — It is a place to write any code thats why we call it as notebook. By the help of Java you can write a code in IntelliJ note book.
What happens inside your workspace when you create your FirstJavaProgram class in your IDE?
Now I have create My firstJavaProgram Class in my eclipse IDE.
If you notice the package explorer my FirstJavaProgram contains .java extension
.java file is also called source file.
This source file contains contains source code.
source code is a plain english text.

Now you need to navigate to your project workspace folder in local system and check for FirstProgram.java file.
Program-> src -> program->FirstProgram.java file.
This FirstProgram.java file contains the actual java code that we have written in java language called source code.

When you save java class in eclipse, the system automatically compiles the .java file and creates a corresponding .class file (with the same name FirstProgram.class ) inside the bin folder of your project workspace.

This .class file contains byte code. Here comes Javac compiler whenever you write any set of instructions and if you right click -> Run as → Java Application.. You will be able to see the output in the console

How are we getting output in the console. In this case, compilation flow and execution flow comes into picture.
Whenever we save the program, Javac compiler comes into picture and generates a .class file containing Byte code and this class file serves as a universal set of instructions for the execution.
Why this byte code is required? — Byte code is required because whenever we run the java program Operating System understands only byte code.
This generated class file can run in any platform. Thats why we call Java as a platform independent.
Execution flow:
Whenever we run the java program, JVM will executes the class file inside the JVM system
we have several filters inside the JVM system. Those are
Class loader — Whenever you run the program, class loader finds the .class file from the bin folder and load them into the computers memory so the program can actually start.
Byte code verifier — Inspects every instructions in the .class file and verifies all the byte code.
Interpreter — It reads the byte code and passes the instructions line by line to to the JVM.
Java Virtual Machine (JVM) — JVM will execute the byte code with the help of Operation System.
Operation System (OS) — Once the execution is done by JVM, Operation system is responsible to decrypt the code to generate the output into source code. After that we are seeing output in the console.


