Difference Between JVM, JRE, and JDK

Soresa

Last Update منذ ٣ أعوام

In this article, we'll discuss differences between JVM, JRE, and JDK by considering their components and uses.


JVM

  • Java Virtual Machine (JVM) is an implementation of a virtual machine which executes a Java program.
  • The JVM first interprets the bytecode. It then stores the class information in the memory area. Finally, it executes the bytecode generated by the java compiler.
  • It is an abstract computing machine with its own instruction set and manipulates various memory areas at runtime.

Components of the JVM are:

  • Class Loaders
  • Run-Time Data Areas
  • Execution Engine


Class Loaders

  • Initial tasks of the JVM includes loading, verifying and linking the bytecode. Class loaders handle these tasks.
  • We have a detailed article specifically on class loaders.


Run-Time Data Areas

  • The JVM defines various memory areas to execute a Java program. These are used during runtime and are known as run-time data areas. Some of these areas are created on the JVM start-up and destroyed when the JVM exits while some are created when a thread is created and destroyed when a thread exits.


Let's consider these areas one by one:


Method Area:

  • Basically, method area is analogous to the storage area for compiled code. It stores structures such as run-time constant pool, field and method data, the code for methods and constructors as well as fully qualified class names.
  • The method area, also known as permanent generation space (PermGen), is created when the JVM starts up. The memory for this area does not need to be contiguous. All the JVM threads share this memory area.



Heap Area:

  • The JVM allocates the memory for all the class instances and arrays from this area.
  • JVM creates heap area as soon as it starts up. All the threads of the JVM share this area. The memory for the heap area does not need to be contiguous.



Stack area:

  • Stores data as frames and each frame stores local variables, partial results and nested method calls. JVM creates the stack area whenever it creates a new thread. 



Execution Engine

Execution engine executes the instructions using information present in the memory areas. It has three parts:


Interpreter:

  • Once classloaders load and verify bytecode, the interpreter executes the bytecode line by line. This execution is quite slow. The disadvantage of the interpreter is that when one method is called multiple times, every time new interpretation is required.
  • However, the JVM uses JIT Compiler to mitigate this disadvantage.



Just-In-Time (JIT) Compiler:

  • JIT compiler compiles the bytecode of the often-called methods into native code at run-time. Hence it is responsible for the optimization of the Java programs.
  • JVM automatically monitors which methods are being executed. Once a method becomes eligible for JIT compilation, it is scheduled for compilation into machine code. This method is then known as a hot method. This compilation into machine code happens on a separate JVM thread.
  • As a result, it does not interrupt the execution of the current program. After compilation into machine code, it runs faster.



Garbage Collector:

  • Java takes care of memory management using Garbage Collection. It's a process of looking at heap memory, identifying which objects are in use and which are not, and finally deleting unused objects.
  • GC is a daemon thread. It can be called explicitly using System.gc() method, however, it won't be executed immediately and the JVM decides when to invoke GC.



Java Native Interface

  • It acts as an interface between the Java code and the native (C/C++) libraries.
  • There are situations in which Java alone doesn't meet the needs for your application, for example, implementing a platform-dependent feature.
  • In those cases, we can use JNI to enable the code running in the JVM to call. Conversely, it enables native methods to call the code running in the JVM.



Native Libraries

These are platform specific libraries and contains the implementation of native methods.



JRE

  • Java Runtime Environment (JRE) is a bundle of software components used to run Java applications.


Core components of the JRE include:

  • An implementation of a Java Virtual Machine (JVM)
  • Classes required to run the Java programs
  • Property Files



Bootstrap Classes

We'll find bootstrap classes under jre/lib/. This path is also known as the bootstrap classpath. It includes:

  • Runtime classes in rt.jar
  • Internationalization classes in i18n.jar
  • Character conversion classes in charsets.jar
  • Others


Bootstrap ClassLoader loads these classes when the JVM starts up.



Extension Classes

  • We can find extension classes in jre/lib/extn/ which acts as a directory for extensions to the Java platform. This path is also known as extension classpath.
  • It contains JavaFX runtime libraries in jfxrt.jar and locale data for java.text and java.util packages in localedata.jar. Users can also add custom jars into this directory.



Property Settings

Java platform uses these property settings to maintain its configuration. Depending on their usage they are located in different folders inside /jre/lib/. These include:

  • Calendar configurations in the calendar.properties
  • Logging configurations in logging.properties
  • Networking configurations in net.properties
  • Deployment properties in /jre/lib/deploy/
  • Management properties in /jre/lib/management/




Other Files

Apart from the above-mentioned files and classes, JRE also contains files for other matters:

  • Security management at jre/lib/security
  • The directory for placing support classes for applets at jre/lib/applet
  • Font related files at jre/lib/fonts and others



JDK

  • Java Development Kit (JDK) provides environment and tools for developing, compiling, debugging, and executing a Java program.


Core components of JDK include:

  • JRE
  • Development Tools
We discussed the JRE in the above section.


Now, we'll focus on various development tools. Let's categorize these tools based on their usage:



Basic Tools

  • These tools lay the foundation of the JDK and are used to create and build Java applications. Among these tools, we can find utilities for compiling, debugging, archiving, generating Javadocs, etc.


They include:

  • javac – reads class and interface definitions and compiles them into class files
  • java – launches the Java application
  • javadoc – generates HTML pages of API documentation from Java source files
  • apt – finds and executes annotation processors based on the annotations present in the set of specified source files
  • appletviewer – enables us to run Java applets without a web browser
  • jar – packages Java applets or applications into a single archive
  • jdb – a command-line debugging tool used to find and fix bugs in Java applications
  • javah – produces C header and source files from a Java class
  • javap – disassembles the class files and displays information about fields, constructors, and methods present in a class file
  • extcheck – detects version conflicts between target Java Archive (JAR) file and currently installed extension JAR files



Security Tools

  • These include key and certificate management tools that are used to manipulate Java Keystores.


  • A Java Keystore is a container for authorization certificates or public key certificates. Consequently, it is often used by Java-based applications for encryption, authentication, and serving over HTTPS.


  • Also, they help to set the security policies on our system and create applications which can work within the scope of these policies in the production environment. These include:
  • keytool – helps in managing keystore entries, namely, cryptographic keys and certificates
  • jarsigner – generates digitally signed JAR files by using keystore information
  • policytool – enables us to manage the external policy configuration files that define installation's security policy.



Internationalization Tool

  • Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes.



Remote Method Invocation (RMI) Tools

  • RMI tools enable remote communication between Java applications thus providing scope for development of distributed applications.



Java Deployment Tools

These tools help in deploying Java applications and applets on the web. They include:

  • pack200 – transforms a JAR file into a pack200 file using the Java gzip compressor
  • unpack200 – transforms pack200 file into a JAR file



Java Plug-in Tool

  • JDK provides us with htmlconverter. Furthermore, it's used in conjunction with the Java Plug-in.
  • On the one hand, Java Plug-in establishes a connection between popular browsers and the Java platform. As a result of this connection, applets on the website can run within a browser.
  • On the other hand, htmlconverter is a utility for converting an HTML page containing applets to a format for Java Plug-in.



Java Web Start Tool

  • JDK brings javaws. We can use it in conjunction with the Java Web Start.
  • This tool allows us to download and launch Java applications with a single click from the browser. Hence, there is no need to run any installation process.



Monitoring and Management Tools

These are great tools that we can use to monitor JVM performance and resource consumption. Here are a few of these: :

  • jconsole – provides a graphical console that lets you monitor and manage Java applications
  • jps – lists the instrumented JVMs on the target system
  • jstat – monitors JVM statistics
  • jstatd – monitors creation and termination of instrumented JVMs



Troubleshooting Tools

These are experimental tools that we can leverage for troubleshooting tasks:

  • info – generates configuration information for a specified Java process
  • jmap – prints shared object memory maps or heap memory details of a specified process
  • jsadebugd – attaches to a Java process and acts as a debug server
  • jstack – prints Java stack traces of Java threads for a given Java process



Conclusion

  • In this article, we identified that the basic difference between JVM, JRE, and JDK lies in their usage.
  • First, we described how the JVM is an abstract computing machine that actually executes the Java bytecode.
  • Then, we explained how to just run Java applications, we use the JRE.
  • And finally, we understood how to develop Java applications, we use the JDK.
  • We also took some time to dig into tools and fundamental concepts of this components.

Was this article helpful?

2 out of 2 liked this article