How Java programs are executed??

How Java programs are executed??

If you have programmed in java, you must have wondered how these java programs are executed inside the compiler and machine. If you are new to java it is necessary to know how the java programs are executed in order to use this programming language efficiently.

HOW JAVA PROGRAMS ARE EXECUTED?

CodeExec.jpg

Java programs are compiled and then they are runned. Firstly java program’s source code is written by the programmer using any text editor and the source code file is saved with .java extension. Then this .java file is compiled using the
javac filename.java command. The compiler compiles this source code into the byte code if there are no errors. Bytecode file has a .class extension. This bytecode can be distributed among different machines and it will run on different operating systems. To run a java program we will need JVM(Java Virtual Machine). There are different JVM editions for different operating systems. To run a java program we will use the java filename.java command. JVM has a class loader which loads the .class file. After loading the bytecode .class file the JVM execution engine executes the bytecode and generates machine code which is run on the machine.

So, what is JVM?

JVM(Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed. JVM is platform dependent i.e each operating system will have a different edition of JVM. JVM is the one that actually calls the main method present in a java code. JVM is a part of JRE(Java Runtime Environment). Whenever you write a java command on the command prompt to run the java class, an instance of JVM is created. Loading,Verification and Execution of code is done by JVM. It provides a runtime environment. JVM provides definitions for memory management, garbage collection, class file format and register set. Components of JVM are discussed below:

Class Loader

Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java: Bootstrap Class Loader, Extension Class Loader and System Application Class Loader. It is mainly responsible for three activities: Loading, Linking and Initialization.

JVM Memory

Method area: In the method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.

Heap area: Information of all objects is stored in heap area. There is also one Heap Area per JVM. It is also a shared resource.

Stack area: For every thread, JVM creates one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which store methods calls. All local variables of that method are stored in their corresponding frame. After a thread terminates, it’s run-time stack will be destroyed by JVM. It is not a shared resource.

PC Registers: Store address of current execution instruction of a thread. Obviously each thread has separate PC Registers. Native method stacks: For every thread, a separate native stack is created. It stores native method information.

Execution Engine

Execution engine executes the .class (bytecode). It reads the byte-code line by line, uses data and information present in various memory areas and executes instructions. It can be classified in three parts :-

Interpreter : It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.

Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter. It compiles the entire bytecode and changes it to native code so whenever interpreter see repeated method calls,JIT provide direct native code for that part so re-interpretation is not required,thus efficiency is improved.

Java Native Interface (JNI) : It is an interface which interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.

Native Method Libraries : It is a collection of the Native Libraries(C, C++) which are required by the Execution Engine.

Execution engine generates the machine specific code and it is transferred to operating system where the program is finally executed on the machine.

JVM vs JRE vs JDK

JDK.png

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.

JRE stands for “Java Runtime Environment” and may also be written as “Java RTE.” The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files. JVM is an implementation of JRE.

I hope this helps you to understand the inner architecture of Java programs. Happy Reading :)