Difference Between JDK vs JRE vs JVM in Java

JDK

The initials JDK stand for “Java Development Kit.” This software package is mostly used to create Java applets and applications. Tools, libraries, and executables required for Java development are all included in the JDK. Java programs can be written, compiled, debugged, and run using these tools.

The JDK typically includes the following components:

Compiler:

The Java compiler, known as javac, transforms Java source code into bytecode, a platform-independent version of the code that can be executed on any device containing a Java Virtual Machine (JVM).

Java Runtime Environment (JRE):

This is a must in order to run Java apps. It contains the JVM as well as the essential libraries and tools needed for running Java programs.

Java Virtual Machine (JVM):

Running Java bytecode on a specific platform is handled by the JVM. In order to make programs truly platform-independent, Java offers an environment that abstracts away the features of the underlying hardware and operating system.

Development Tools:

A variety of development tools are included with the JDK, including the Java Debugger (jdb) for debugging Java code, the Java Archive tool (jar) for creating and managing JAR files, and the Java Documentation tool (javadoc) for generating API documentation from source code comments.

Libraries and APIs:

For a wide range of functionalities, including networking, file I/O, user interface (UI) development, and others, the JDK offers a complete set of libraries and APIs. The Java Standard Library includes these libraries.

Sample Code and Documentation:

To aid developers in learning about Java capabilities and best practices, the JDK frequently provides sample code and documentation.

It’s important to note that there are different versions of the JDK available, each corresponding to a specific version of the Java programming language. Newer versions of the JDK typically come with improvements, new features, and bug fixes.

Developers can choose the appropriate version of the JDK based on their project’s requirements and compatibility considerations.

  public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

1. Install JDK: Verify your computer has the Java Development Kit (JDK) installed. You may either utilize other distributions like OpenJDK or get it from the official Oracle website.

2. Write the Code: Create a file named HelloWorld.java and paste the above code into it using a text editor.

3. Compile the Code: Open a terminal or command prompt, go to the directory that contains the HelloWorld.java file, and run the following command to compile the code: javac HelloWorld.java

This will generate a file named HelloWorld.class, which contains the compiled bytecode.

Run the Program: After successful compilation, run the program

JDK Architecture

JRE

The initials JRE stand for “Java Runtime Environment.” It is a piece of software that offers the environment needed to run Java programs and applets. The Java Runtime Environment (JRE), which is a part of the Java Development Kit (JDK), contains the elements needed to run Java programs without the need for development tools.

jdk image

Java Virtual Machine (JVM):

The JVM is a crucial component of the JRE. It’s responsible for executing Java bytecode on a specific platform. The JVM abstracts the underlying hardware and operating system details, allowing Java programs to be truly platform-independent. It manages memory, performs garbage collection, and provides an execution environment for Java applications.

Core Libraries:

The JRE includes a set of core libraries and classes that provide fundamental functionality to Java programs. These libraries cover various areas such as input/output operations, data structures, networking, math calculations, and more.

Class Loader:

The class loader within the JRE loads Java classes at runtime, allowing classes to be dynamically loaded as needed during program execution. This feature contributes to the flexibility and extensibility of Java applications.

Security Components:

The JRE includes security features to ensure that Java programs run in a secure environment. It includes mechanisms for controlling access to system resources, handling digital signatures, and enforcing security policies.

Native Libraries:

Some Java programs may require interaction with native libraries or system-specific functionality. The JRE provides a bridge for Java code to interface with these native libraries.

Executable Files:

The JRE includes executable files, such as java (used to launch Java applications) and javaw (used for launching applications without a command prompt window on Windows systems).

When you want to run a Java application on your computer, you typically need to have the appropriate version of the JRE installed. Developers often test their applications with a specific version of the JRE to ensure compatibility and correct behavior.

It’s worth noting that the JRE is a subset of the JDK; if you’re a developer, you’ll need the full JDK to compile and develop Java applications, whereas end-users usually only need the JRE to run them.

Here’s an example of a simple Java program that demonstrates the use of the Java Runtime Environment (JRE) by running a basic “Hello, World!” application:

public class JREExample {
    public static void main(String[] args) {
        System.out.println("Hello from the Java Runtime Environment!");
    }
}

Here’s how you can run this example:

1. Make sure you have the JRE installed on your system. The JRE is typically installed automatically when you install a Java application or JDK.

2. Create a file named JREExample.java and paste the code into it.

3. Open a terminal or command prompt, go to the directory containing the JREExample.java file, and use the javac command to build it: javac JREExample.java

Run the compiled program using the Java command.

You should see the output “Hello from the Java Runtime Environment!” printed on the console.

jre image

JRE Architecture

JRE working:

The JVM (Java Virtual Machine) serves as an operational engine for executing Java applications. It is responsible for initiating the execution of the main method within Java code. JVM is an integral component of the JRE (Java Runtime Environment).

Java applications adhere to the principle of WORA (Write Once Run Anywhere), which signifies that programmers can develop code on one system and anticipate it to function on any other Java-enabled system without necessitating modifications. This seamless cross-system compatibility is achievable due to the presence of JVM.

Upon compiling a .java file, the Java compiler generates .class files containing bytecode, retaining the same class names as in the .java file. When these .class files are executed, they undergo a series of stages, collectively defining the complete functionality of the JVM.

jre class louder

JVM

VM stands for “Java Virtual Machine.” It’s a fundamental component of the Java platform that provides an execution environment for Java applications and allows them to run on different hardware and operating systems without modification.

Here are the key aspects of the Java Virtual Machine:

Platform Independence:

The JVM’s capacity to offer platform independence is one of its key characteristics. In order to create bytecode, which is a platform-independent intermediate representation, Java source code must be compiled.

The JVM then interprets or compiles this bytecode into native machine code for the specific platform it’s running on. This enables Java applications to be written once and run anywhere (the “write once, run anywhere” principle).

Memory Management:

The JVM manages memory allocation and deallocation for Java applications. It performs automatic memory management, including garbage collection, which helps reclaim memory occupied by objects that are no longer in use, thus preventing memory leaks.

Execution of Bytecode:

The JVM interprets or compiles bytecode instructions into native machine code that the host operating system can execute. Early versions of the JVM heavily relied on bytecode interpretation, while more modern versions use Just-In-Time (JIT) compilation to dynamically convert bytecode into native machine code for improved performance.

Class Loading:

The JVM’s class loader subsystem loads classes as needed during program execution. This allows for dynamic loading and linking of classes at runtime, which contributes to the flexibility and extensibility of Java applications.

Security and Sandbox:

The JVM enforces security features to provide a secure execution environment for Java applications. It uses a security manager to control access to system resources and prevent unauthorized actions.

Java Native Interface (JNI):

The JVM provides the Java Native Interface, which allows Java code to call native code written in languages like C and C++. This is useful for tasks that require interaction with platform-specific features or libraries.

Multithreading:

The JVM supports multithreading, allowing Java programs to execute multiple threads concurrently. The JVM manages thread scheduling and synchronization.

Performance Optimization:

Modern JVMs often include various performance optimization techniques, including adaptive JIT compilation, runtime profiling, and efficient memory management strategies.

Here’s a simple Java program that demonstrates the use of the Java Virtual Machine (JVM) through a basic example of a custom class and object instantiation:

  public class JVMExample {
    public static void main(String[] args) {
        // Creating an instance of the CustomClass
        CustomClass instance = new CustomClass();
        
        // Calling methods on the instance
        instance.printMessage();
        int result = instance.addNumbers(5, 7);
        System.out.println("Sum: " + result);
    }
}

class CustomClass {
    public void printMessage() {
        System.out.println("Hello from CustomClass!");
    }
    
    public int addNumbers(int a, int b) {
        return a + b;
    }
}

In this example, we define a simple class called CustomClass that contains methods to print a message and add two numbers. The main JVMExample class demonstrates how the JVM manages objects, method calls, and their execution.

Here’s how you can run this example:

  • Make sure you have the Java Development Kit (JDK) installed on your system.
  • Create a file named JVMExample.java and paste the code into it.
  • Open a command prompt or terminal, navigate to the directory containing the JVMExample.java file, and compile it using the javac command: javac JVMExample.java
  • Run the compiled program using the java command:java JVMExample
  • You’ll see the output showing the message “Hello from CustomClass!” and the sum of two numbers (5 + 7) as “Sum: 12”.
  • This example demonstrates how the JVM manages classes, objects, method calls, and their execution, showcasing the foundational principles of object-oriented programming in Java.

jvm class louder

JDK vs. JVM vs. JRE

Aspect JDK (Java Development Kit) JVM (Java Virtual Machine)   JRE (Java Runtime Environment)
 Purpose Development and compilation of Java applications. Execution of Java bytecode. Running Java applications.
 Components    Included Compiler, debugger, libraries, and other tools for Java development. Core runtime, execution engine, and runtime libraries for Java applications. Core runtime, execution engine, and necessary libraries to run Java applications.
        

 Usage

Used by developers for creating, compiling, and testing Java programs. Executes Java bytecode and manages memory, threads, and resources during program execution. Required to run compiled Java applications.
 Main Components -javac(Java Compiler)<br>

-java (Launcher)<br>

– Debugger<br>

– Java libraries

-Class Loader<br>

-Bytecode Verifier<br>

-Execution Engine<br>

– Native Interface

-Core Runtime<br>

-Class Libraries<br>

– java command (Launcher)

 Compatibility Includes the JRE, so any Java program compiled with JDK can be executed using JRE. Dependent on the JDK used for compilation. Allows running Java applications without development tools.
 java Necessity for Users Essential for Java developers who need to write, compile, and test code. Integral for executing Java applications, but end-users do not need to install it separately. Required for running Java applications, but end-users do not need to compile or develop code.

Conclusion

In a nutshell, the trio of JVM, JDK, and JRE exemplifies the modular nature of the Java ecosystem. Developers leverage the JDK’s tools to create applications, which are then executed by the JVM on various platforms, thanks to the JRE. This Synergy highlights Java’s position as one of the most adaptable and frequently used programming languages in the world by embodying its promise of consistency and portability.

Understanding the functions of JVM, JDK, and JRE remains a cornerstone for anybody entering the world of Java programming as the technological environment continues to change.