Core Java

JVM Architecture: Execution Engine in JVM

Hello readers! In the previous article of JVM series, developers learned about the Virtual machine’s ClassLoader and Runtime Data Areas components. This tutorial will help developers to correctly understand the Execution Engine in JVM.

1. Introduction

Before moving further let’s take a look at the Java Virtual Machine and its basic characteristics.

1.1 What is Java Virtual Machine (JVM)?

Java Virtual Machine (JVM) is an abstract virtual machine that resides on your computer and provides a runtime environment for the Java bytecode to get executed. JVM is available for many hardware and software platforms but few Java developers know that the Java Runtime Environment (JRE) is the implementation of the Java Virtual Machine (JVM). JVM analyze the bytecode, interprets it, and execute the same bytecode to display the output.

The basic function of JVM is to execute the compiled .class files (i.e. the bytecode) and generate an output. Do note, each operating system has a different JVM, but the generated bytecode output is the same across all operating systems. This means that the bytecode generated on Windows OS can also run on Linux OS and vice-versa, thus making Java as a platform independent language.

Fig. 1: An overview of Java Virtual Machine
Fig. 1: An overview of Java Virtual Machine

1.1.1 What JVM does?

Java Virtual machine performs the following operations:

  • Loading of the required of the required .class and jar files
  • Assigning references and verification of the code
  • Execution of the code
  • Provides a runtime environment for the Java bytecode

1.1.2 JVM Internal Architecture

The following diagram shows the key internal components of Java Virtual Machine that conforms to the JVM specification.

Fig. 2: Java Virtual Machine architecture
Fig. 2: Java Virtual Machine architecture

The execution engine component shown in Fig. 2 is each explained below.

1.2 Execution Engine in JVM

This component executes the bytecode which is assigned to the runtime data areas through the classloader. This bytecode is read as instructions and each bytecode instruction consists of a 1-byte OpCode and an addition operand. Do note, that the Java bytecode is written in a human-readable form, thus the execution engine must change the bytecode to a language that can be easily executed by the machine in the Java Virtual Machine. The execution engine has three major sub-components i.e.

  • Interpreter: This component reads the bytecode instructions and executes them in a sequential manner. The component runs the application from the command line by accepting a filename argument. The following prototype command can be used:
    java <_compiled_file_name_without_extension_>
    

    Do remember, the compiled class (i.e. .class file) loaded by the virtual machine interpreter must contain a main() method that takes the below form.

    public static void main(String[] args) { 
    
    // Sample code here
    
    }
    
  • JIT (Just In Time) Compiler: This component counterbalance the Interpreter’s disadvantage of slow execution and improves the performance. JIT compiler compiles the similar part of the bytecode at the same time and thus reduces the total time needed for compilation. The compiler in this component refers to a translator which converts the JVM instruction set to the OS-specific instruction set.
    • Oracle uses a compiler known as Hotspot Compiler which searches for hotspots that require compiling with the highest priority through profiling and then compiles the hotspot to the native code. In here, if the bytecode of a compiled method is no longer required, the hotspot virtual machine removes the native code from the cache and runs in the interpreter mode
  • Garbage Collection: This component is a part of execution engine which frees up the memory by collecting and removing the unreferenced objects

Fig. 3: Execution Engine in JVM
Fig. 3: Execution Engine in JVM

1.3 How to Compile and Execute a Java class?

This section will demonstrate the compilation and execution of a Java class. Let’s understand this process with the help of sample code snippet.

1.3.1 Creating a Java file

Open the operating system command prompt and we will use the ‘notepad’ to create a simple Java class. The following Java command can be used.

> notepad _sample_file_name_with_extension_

The command gives the below output.

Fig. 4: Creating a Java file using Notepad
Fig. 4: Creating a Java file using Notepad

1.3.2 Writing the sample Java Code

As shown in Fig. 4, the command will open a notepad and developers can add the sample code to the Welcome.java file that displays a dummy output. The sample code is shown in Fig. 5 and will display a greetings message on successful execution.

Fig. 5: Adding the sample code
Fig. 5: Adding the sample code

1.3.3 Compiling the Java class

After saving the code in Welcome.java file, developers will need to compile it. This compilation will produce the Welcome.class file which in turn will generate a .class file. To compile the file, the following Java command can be used.

> javac _Java_file_name_with_extension_

The command gives the below output.

Fig. 6: Compiling the file
Fig. 6: Compiling the file

1.3.4 Executing the Java class

Now, developers will need to execute the generated Welcome.class file to display the output. To execute the file, the following Java command can be used.

> java _Compiled_file_name_with_extension_

The command gives the below output.

Fig. 7: Executing the file
Fig. 7: Executing the file

1.4 JVM vs. JRE vs. JDK

Before we go ahead and complete this tutorial, let take a look at the three important keywords of Java programming. Many developers fail to get the difference between them.

  • Java Virtual Machine (JVM): JVM is a virtual machine which provides a runtime environment for executing the Java bytecode
  • Java Runtime Environment (JRE): JRE is an environment within which the JVM runs and has class libraries and other files that Java Virtual Machine uses at the time of execution. In other words, JRE = Java Virtual Machine (JVM) + Libraries to run the application
  • Java Development Kit (JDK): JDK is the parent set of the JRE and has everything that JRE contains along with the development tools such as a compiler, debugger etc. In other words, JDK = Java Runtime Environment (JRE) + Development tools

Here is the pictorial representation of JVM, JRE, and JDK.

Fig. 8: JVM vs. JRE vs. JDK Representation
Fig. 8: JVM vs. JRE vs. JDK Representation

That’s all for this post. Happy Learning!

2. Conclusion

In this tutorial, developers had an overview of the execution engine component in JVM. You can download the sample code in the Downloads section.

3. Download the Source Code

This was an overview tutorial of Execution engine in Java Virtual Machine (JVM).

Download
You can download the source code of this tutorial here: Compilation_and_Execution

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button