The differences and connections between JDK, JRE and JVM

The relationship between the three

As far as the relationship between the three is concerned, jvm is a subset of jre, and jre is a subset of jdk. The specific relationship is as follows:

Java execution flow

For a Java program, its execution flow is roughly as follows:

  1. Developers use JDK to write and compile Java source code to generate Java bytecode files (.class files).

  2. On the target computer, run the Java program through the command line or integrated development environment (IDE). At this point, the JVM will load and execute Java bytecode (.class file).

  3. The JVM executes the Java program, executes various methods and statements according to the logic of the program, and calls the class library in the JRE.

  4. During program execution, the JVM is responsible for memory management, garbage collection, handling exceptions, and mapping to the local system.

The following is a detailed explanation of the concepts of these three 

JDK (Java Development Kit) :

  1. Java compiler (javac) : A tool used to compile Java source code files (.java files) into Java bytecode files (.class files). This is a critical step in converting source code into an executable program.

  2. Java Virtual Machine (JVM) : The JDK contains the Java Virtual Machine, which is responsible for executing Java bytecode on the computer. The JVM is the runtime engine for Java applications and is responsible for loading bytecode and converting it into machine code.

  3. Java core class library : JDK includes the core class library of the Java programming language. These libraries contain many commonly used classes and methods for various tasks, such as string processing, collection operations, file operations, input and output, etc.

  4. Development tools : JDK provides a variety of development tools, including debuggers (jdb), document generation tools (javadoc), performance analyzers, decompilers, etc. These tools help developers write, test and optimize Java code.

  5. Header files and development libraries : The JDK contains header files and development libraries that are used to develop native methods and functions related to the Java Native Interface (JNI). This allows Java code to interact with native libraries, thereby extending Java's functionality.

  6. Sample code and demos : The JDK includes sample code and demos that help developers learn how to use different Java classes and libraries.

  7. API documentation : The JDK includes complete Java API documentation, which can be accessed locally or online. These documents provide detailed information about Java classes, methods, and packages, as well as sample code and usage instructions.

  8. Other Tools and Utilities : The JDK also contains other utilities and utilities, such as the keystore management tool (keytool) for managing digital certificates and keystores, and the deployment tool for deploying applications to servers.

JRE (Java Runtime Environment) :

  1. Java Virtual Machine (JVM) : JVM is the core component of JRE, which is responsible for executing Java bytecode and managing the runtime environment of Java programs.

  2. Core Libraries : JRE includes a series of core libraries that contain the basic functions and tools of the Java programming language. Some important core libraries include:

    • java.lang: Provides basic classes and exception handling mechanisms of the Java language.
    • java.util: Contains various utility classes, such as collection frameworks (such as ArrayList, HashMap) and date and time processing classes.
    • java.io: Class library for input and output operations, including file operations and stream processing.
    • java.net: Class library for network communication, supporting the creation of network connections and data transmission.
  3. AWT and Swing (user interface libraries) : The JRE includes Abstract Window Toolkit (AWT) and Swing, libraries for creating graphical user interface (GUI) applications. They provide a variety of components and tools that allow developers to build interactive window applications.

  4. Java core package (Java API) : JRE also includes other Java APIs for accessing functions and services in different areas, such as database access (JDBC), XML processing (Java XML API), security (Java Security API), etc.

  5. Java runtime support : JRE includes a runtime environment that supports Java programs, including memory management, garbage collection, thread management, etc. These components ensure the stability and performance of Java programs.

  6. Some standard extensions (Optional Extensions) : JRE can also include some optional extensions that provide additional functionality, such as Java Naming and Directory Interface (JNDI), Java Authentication and Authorization Service (JAAS), etc. These extensions are typically not part of all JRE implementations, but are installed or configured as needed.

JVM (Java Virtual Machine) :

  1. Class Loader :

    • The class loader is responsible for loading class files and converting them into data structures inside the JVM. JVM supports multiple class loaders, including startup class loader, extension class loader and application class loader, which load class files according to different class paths.
  2. Runtime Data Areas :

    • The JVM includes several runtime data areas that store data and information needed during program execution. These data areas include:
      • Method Area : used to store structural information, static variables, constant pools, etc. of the class.
      • Heap : used to store object instances, including objects created by programmers and objects created by the Java virtual machine (such as string pools and instances of classes).
      • Virtual Machine Stack (Java Virtual Machine Stack) : Each thread has a virtual machine stack, which is used to store local variables and partial calculation results of methods, as well as operations for method calls and returns.
      • Native Method Stack : Similar to the virtual machine stack, but used for the execution of local methods (methods called through the JNI interface).
      • Program Counter : Used to indicate the address of the instruction being executed by the current thread.
  3. Execution Engine :

    • The execution engine is responsible for executing Java bytecode. It includes an interpreter and a just-in-time compiler (JIT compiler) that converts bytecode into native machine code for faster execution.
  4. Native Interface :

    • The JVM provides a native interface that allows Java programs to interact with native code (such as C/C++). This is achieved through Java Native Interface (JNI).
  5. Native Method Libraries :

    • The JVM includes a set of native method libraries that contain native implementations of some methods in the Java standard library, as well as other functions related to the underlying operating system and hardware.
  6. Security and Memory Management :

    • The JVM is responsible for memory management, including automatic memory collection (garbage collection) to release objects that are no longer used. It also implements many security features such as class loader security, access control, and sandbox security.
  7. Multithreading Support :

    • The JVM allows Java programs to create and manage multiple threads for concurrent execution of code.
  8. Garbage Collector :

    • The garbage collector is the part of the JVM that is responsible for identifying and recycling objects that are no longer referenced to free up memory space.

Guess you like

Origin blog.csdn.net/qq_51118755/article/details/133193802