What is JDK, JRE, and JVM

Photo of

Letโ€™s take a closer look at JDK, JRE, and JVM to understand the function of each.

jdk-jre-jvm

๐—๐——๐—ž (๐—๐—ฎ๐˜ƒ๐—ฎ ๐——๐—ฒ๐˜ƒ๐—ฒ๐—น๐—ผ๐—ฝ๐—บ๐—ฒ๐—ป๐˜ ๐—ž๐—ถ๐˜)

What is JDK?

JDK is like a toolkit for Java developers. It contains tools, executables, and binaries needed for Java development, including compilers, debuggers, and other utilities.

What does JDK include?

JDK includes the JRE (Java Runtime Environment), development tools, libraries, header files, and documentation.

Hereโ€™s a comprehensive list of whatโ€™s included in the JDK.

Development Tools

  • javac: The Java compiler used to compile Java source code into bytecode.
  • javap: The Java Class File Disassembler used to disassemble compiled class files.
  • javadoc: The Java documentation generator used to create API documentation from source code comments.

Execution Tools

  • java: The Java Virtual Machine (JVM) launcher that executes compiled Java applications.
  • jjs (JShell): The interactive command-line tool for evaluating Java expressions and statements. (deprecated in JDK 11)

Packaging Tools

  • jar: The Java Archive tool used to package files into a Java Archive (JAR) file.
  • javapackager: The tool for packaging and deploying Java applications, including self-contained applications.

Debugging Tools

  • jdb: The Java Debugger used for debugging Java applications.
  • jstack: The utility to print Java thread stack traces for a given Java process.

Monitoring and Management Tools

  • jconsole: The Java Monitoring and Management Console used to monitor and manage Java applications.
  • jstat: The Java Virtual Machine statistics monitoring tool.

Security Tools

  • keytool: The tool for managing keystores and certificates for secure communication.
  • jarsigner: A tool to sign and verify Java Archive (JAR) files.

Miscellaneous Tools

  • extcheck: A utility to detect version conflicts between a Java application and installed extensions.

Checkout all the tools included in OpenJDK @ https://openjdk.org/tools

What is the use of JDK?

Developers use JDK to write, compile, and debug Java applications.


๐—๐—ฅ๐—˜ (๐—๐—ฎ๐˜ƒ๐—ฎ ๐—ฅ๐˜‚๐—ป๐˜๐—ถ๐—บ๐—ฒ ๐—˜๐—ป๐˜ƒ๐—ถ๐—ฟ๐—ผ๐—ป๐—บ๐—ฒ๐—ป๐˜)

What is JRE?

JRE is like a runtime environment where Java programs run. It provides the necessary runtime libraries and components for executing Java applications.

JRE is a complete runtime environment that provides everything needed to run Java applications, including the JVM, core libraries, supporting APIs, tools, and utilities.

It creates an environment in which compiled Java bytecode can be executed and interact with the system, while leveraging the comprehensive Java API for various functionalities.

What does JRE include?

JRE includes the JVM (Java Virtual Machine), core libraries, and other supporting files.

Core Libraries (Java API)

The JRE includes the core Java libraries, which provide the fundamental building blocks for Java applications.

These libraries encompass a wide range of classes and APIs for data structures, I/O operations, networking, GUI components, concurrency, and more.

Java Native Interface (JNI)

JNI is a programming framework that allows Java code to call and be called by native applications or libraries written in other programming languages like C or C++.

The JRE includes support for JNI, enabling interaction between Java and native code.

Java Accessibility API

This API provides accessibility support, making it possible for Java applications to be used by individuals with disabilities.

It includes features like screen readers and keyboard navigation.

Security Components

The JRE includes security components to ensure a secure execution environment for Java applications.

This includes security providers, cryptographic algorithms, and mechanisms to handle digital certificates and permissions.

Internationalization and Localization Support

The JRE includes features and tools to support internationalization and localization, allowing Java applications to be adapted for different languages and regions.

What is the use of JRE?

End-users use JRE to run Java applications on their machines.


๐—๐—ฉ๐—  (๐—๐—ฎ๐˜ƒ๐—ฎ ๐—ฉ๐—ถ๐—ฟ๐˜๐˜‚๐—ฎ๐—น ๐— ๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ฒ)

What is JVM?

JVM is like a virtual computer that executes compiled Java bytecode. It translates bytecode (instructions) into machine-specific instructions and manages the programโ€™s execution.

What does JVM do?

JVM interprets Java bytecode and executes it by interacting with the system hardware and operating system.

When you write Java code, itโ€™s first compiled into bytecode by the Java compiler.

Bytecode is a platform-independent representation of your code.

There is a JVM implementation specific to each platform (Windows, Mac, Linuxโ€ฆ)

The JVM knows nothing about the Java programming language, but only of a particular binary format, theย classย file format.

The JVM loads classes from the bytecode as needed.

Then the Execution engine of JVM converts the bytecode to native machine instructions and executes the program.

Main components of JVM

Class Loader Subsystem

The class loader has three components.

  1. Bootstrap Class Loader
  2. Extension Class Loader
  3. Application Class Loader

Hereโ€™s what they do:

  1. Bootstrap Class Loader

Loads core Java classes like java.lang.Object and other runtime classes essential for JVM operation.

  1. Extension Class Loader

Loads classes from the Java extension directory (usually $JAVA_HOME/lib/ext).

eg: classes from javax libraries such as javax.xml.

  1. Application Class Loader

Loads classes from the application classpath specified by the CLASSPATH environment variable or the -cp option.

Runtime Data Areas

  • Method Area (Class Area)

    Stores class metadata, static fields, and method information. Each loaded class has its own method area.

  • Heap

    Stores objects and their instance variables. Itโ€™s shared among all threads and is where garbage collection occurs.

  • Stack

    Stores local variables, method call information, and partial results.

    Each thread has its own stack, created along with the thread. It includes the method call stack (frames) and local variables.

  • PC (Program Counter) Register

    Holds the address of the JVM instruction being executed.

  • Native Method Stack

    Stores native method information.

Execution Engine

Execution engine consists of Interpreter and Just-In-Time (JIT) Compiler.

  • Interpreter

    Reads and interprets bytecode line by line and executes the corresponding native instructions. Slower compared to the JIT compiler but platform-independent.

  • Just-In-Time (JIT) Compiler

    Translates bytecode into native machine code specific to the underlying hardware, optimizing performance.

    The compiled code is cached for future use.

Garbage Collector (GC)

Manages memory by reclaiming memory that is no longer in use (garbage collection).

Native Interface

JNI (Java Native Interface) allows Java code to call and be called by native applications or libraries written in languages like C or C++.

Native Method Libraries

Libraries of native methods that provide implementations for native (platform-dependent) methods.

These components work together to load classes, manage memory, execute bytecode, and interface with native code, enabling Java applications to run in a platform-independent manner.

The JVM provides a consistent runtime environment across various operating systems and hardware architectures, allowing Java applications to run reliably and efficiently.

This is a very abstract and simplified overview of the JVM and its components.
Thereโ€™s lot more to explore in each sub component.

JVM is a key component of JRE. When you run a Java application, the JRE starts the JVM to execute the applicationโ€™s bytecode.


Following image is a screenshot from the Java Documentation

jdk


The difference between Process and Threads


The difference between Map and FlatMap methods in Java Stream API