Difference Between JVM, JRE, and JDK – JVM、JRE和JDK之间的区别

最后修改: 2018年 6月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

在这篇文章中,我们将通过考虑JVM、JRE和JDK的组件和用途来讨论它们之间的区别。

2. JVM

2.JVM

Java Virtual Machine (JVM) is an implementation of a virtual machine which executes a Java program.

Java虚拟机(JVM)是一个执行Java程序的虚拟机的实现。

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.

JVM首先解释字节码。然后,它将类信息存储在内存区。最后,它执行由java编译器生成的字节码。

It is an abstract computing machine with its own instruction set and manipulates various memory areas at runtime.

它是一个抽象的计算机,有自己的指令集,并在运行时操纵各种内存区域。

Components of the JVM are:

JVM的组成部分是。

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

2.1. Class Loaders

2.1.类加载器

Initial tasks of the JVM includes loading, verifying and linking the bytecode. Class loaders handle these tasks.

JVM的初始任务包括加载、验证和链接字节码。类加载器处理这些任务。

We have a detailed article specifically on class loaders.

我们有一篇专门关于class loaders的详细文章。

2.2. Run-Time Data Areas

2.2.运行时数据区域

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.

JVM定义了各种内存区域来执行Java程序。这些区域在运行期间使用,被称为运行时数据区域。其中一些区域在JVM启动时被创建,并在JVM退出时被销毁,而一些区域在线程创建时被创建,并在线程退出时被销毁。

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 JVM stores these structure for each and every class.

基本上,方法区类似于编译后代码的存储区。它存储的结构包括运行时常量池、字段和方法数据、方法和构造函数的代码,以及完全合格的类名。JVM为每一个类存储这些结构。

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.

方法区,也被称为永久生成空间(PermGen),在JVM启动时被创建。这个区域的内存不需要是连续的。所有的JVM线程都共享这个内存区域。

Heap Area

堆积区

The JVM allocates the memory for all the class instances and arrays from this area.

JVM从这个区域为所有的类实例和数组分配内存。

Garbage Collector (GC) reclaims the heap memory for objects. Basically, GC has three phases to reclaim memory from objects viz. two minor GC and one major GC.

垃圾收集器(GC)为对象回收堆内的内存。基本上,GC有三个阶段来回收对象的内存,即两个小GC和一个大GC。

The heap memory has three portions:

堆内存有三个部分。

  • Eden Space – it’s a part of Young Generation space. When we create an object, the JVM allocates memory from this space
  • Survivor Space – it’s also a part of Young Generation space. Survivor space contains existing objects which have survived the minor GC phases of GC
  • Tenured Space – this is also known as the Old Generation space. It holds long surviving objects. Basically, a threshold is set for Young Generation objects and when this threshold is met, these objects are moved to tenured space.

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.

JVM一启动就会创建堆区。JVM的所有线程都共享这个区域。堆区的内存不需要是连续的。

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. This area is private for each thread.

以框架形式存储数据,每个框架存储局部变量、部分结果和嵌套方法调用。JVM每当创建一个新的线程时都会创建堆栈区域。这个区域对每个线程都是私有的。

Each entry in the stack is called Stack Frame or Activation record. Each frame contains three parts:

堆栈中的每个条目都被称为堆栈框架或激活记录。每个框架包含三个部分。

  • Local Variable Array – contains all the local variables and parameters of the method
  • Operand Stack – used as a workspace for storing intermediate calculation’s result
  • Frame Data – used to store partial results, return values for methods, and reference to the Exception table which provides corresponding catch block information in case of exceptions

The memory for the JVM stack does not need to be contiguous.

JVM堆栈的内存不需要是连续的。

PC Registers

PC寄存器

Each JVM thread has a separate PC Register which stores the address of the currently executing instruction. If the currently executing instruction is a part of the native method then this value is undefined.

每个JVM线程都有一个单独的PC寄存器,用于存储当前执行的指令的地址。如果当前执行的指令是本地方法的一部分,那么这个值是未定义的。

Native method stacks

本机方法堆栈

Native methods are those which are written in languages other than Java.

本地方法是指那些用Java以外的语言编写的方法。

JVM provides capabilities to call these native methods. Native method stacks are also known as “C stacks”. They store the native method information. Whenever the native methods are compiled into machine codes, they usually use a native method stack to keep track of their state.

JVM提供了调用这些本地方法的能力。本地方法栈也被称为 “C栈”。它们存储了本地方法的信息。每当本地方法被编译成机器代码时,它们通常使用本地方法栈来跟踪其状态。

The JVM creates these stacks whenever it creates a new thread. And thus JVM threads don’t share this area.

JVM每当创建一个新的线程时都会创建这些堆栈。因此,JVM的线程不会共享这个区域。

2.3. Execution Engine

2.3.执行引擎

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.

然而,JVM使用JIT编译器来缓解这一缺点。

Just-In-Time (JIT) Compiler

准时制(JIT)编译器

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.

JIT编译器在运行时将经常调用的方法的字节码编译成本地代码。因此,它负责对Java程序进行优化。

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.

JVM自动监控哪些方法正在被执行。一旦一个方法符合JIT编译的条件,它就会被安排编译成机器代码。这个方法就被称为热方法。编译成机器代码的过程发生在一个单独的JVM线程上。

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.

Java使用垃圾收集来处理内存管理。这是一个查看堆内存的过程,识别哪些对象正在使用,哪些没有,最后删除未使用的对象。

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.

GC是一个守护线程。它可以使用System.gc()方法显式调用,但是,它不会被立即执行,JVM会决定何时调用GC。

2.4. Java Native Interface

2.4.Java本地接口

It acts as an interface between the Java code and the native (C/C++) libraries.

它作为Java代码和本地(C/C++)库之间的一个接口。

There are situations in which Java alone doesn’t meet the needs for your application, for example, implementing a platform-dependent feature.

在有些情况下,单靠Java不能满足你的应用需求,例如,实现一个依赖平台的功能。

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.

在这些情况下,我们可以使用JNI来使运行在JVM中的代码能够被调用。反之,它可以使本地方法调用在JVM中运行的代码。

2.5. Native Libraries

2.5.本地图书馆

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

这些是特定平台的库,包含了本地方法的实现。

3. JRE

3.JRE

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

Java Runtime Environment(JRE)是用于运行Java应用程序的一揽子软件组件。

Core components of the JRE include:

JRE的核心组件包括。

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

We discussed the JVM in the above section. Here we will focus on the core classes and support files.

我们在上一节中讨论了JVM。这里我们将重点讨论核心类和支持文件。

3.1. Bootstrap Classes

3.1.Bootstrap类

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

我们将在jre/lib/下找到bootstrap类。这个路径也被称为bootstrap classpath。它包括。

  • 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.

Bootstrap ClassLoader在JVM启动时加载这些类。

3.2. Extension Classes

3.2.扩展类

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.

我们可以在jre/lib/extn/中找到扩展类,它作为Java平台的一个扩展目录。这个路径也被称为扩展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.

它包含jfxrt.jar中的JavaFX运行库,以及java.textjava.util软件包localedata.jar中的区域数据。用户也可以在这个目录中添加自定义的罐子。

3.3. Property Settings

3.3.属性设置

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:

Java平台使用这些属性设置来维护其配置。根据其用途,它们位于/jre/lib/内的不同文件夹中。这些文件夹包括

  • 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/

3.4. Other Files

3.4.其他文件

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

除了上述文件和类之外,JRE还包含其他事项的文件。

  • 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

4. JDK

4.JDK

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

Java开发工具包(JDK)为开发、编译、调试和执行Java程序提供环境和工具。

Core components of JDK include:

JDK的核心组件包括。

  • JRE
  • Development Tools

We discussed the JRE in the above section.

我们在上一节中讨论了JRE。

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

现在,我们将集中讨论各种开发工具。让我们根据这些工具的用途进行分类。

4.1. Basic Tools

4.1.基本工具

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.

这些工具奠定了JDK的基础,用于创建和构建Java应用程序。在这些工具中,我们可以找到用于编译、调试、归档、生成Javadocs等的实用程序

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

4.2. Security Tools

4.2.安全工具

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

这些包括用于操作Java密钥库的密钥和证书管理工具。

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.

Java Keystore是一个授权证书或公共密钥证书的容器。因此,它经常被基于Java的应用程序用于加密、认证和通过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

Some security tools also help in managing Kerberos tickets.

一些安全工具也有助于管理Kerberos票据。

Kerberos is a network authentication protocol.

Kerberos是一个网络认证协议。

It works on the basis of tickets to allow nodes communicating over a non-secure network to prove their identity to one another in a secure manner:

它以票据为基础,允许在非安全网络上通信的节点以安全的方式向对方证明其身份。

  • kinit – used to obtain and cache Kerberos ticket-granting tickets
  • ktab – manages principle names and key pairs in the key table
  • klist – displays entries in the local credentials cache and key table

4.3. Internationalization Tool

4.3.国际化工具

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

国际化是设计一个应用程序的过程,以便它能够适应各种语言和地区,而不需要工程上的改变。

For this purpose, the JDK brings native2ascii. This tool converts a file with characters supported by JRE to files encoded in ASCII or Unicode escapes.

为了这个目的,JDK带来了native2ascii。这个工具可以将带有JRE支持的字符的文件转换为以ASCII或Unicode转义编码的文件。

4.4. Remote Method Invocation (RMI) Tools

4.4.远程方法调用(RMI)工具

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

RMI工具使Java应用程序之间能够进行远程通信,从而为开发分布式应用程序提供了空间。

RMI enables an object running in one JVM to invoke methods on an object running in another JVM. These tools include:

RMI使在一个JVM中运行的对象能够调用在另一个JVM中运行的对象的方法。这些工具包括。

  • rmic – generates stub, skeleton, and tie classes for remote objects using the Java Remote Method Protocol (JRMP) or Internet Inter-Orb Protocol (IIOP)
  • rmiregistry – creates and starts remote object registry
  • rmid – starts the activation system daemon. This allows objects to be registered and activated in a Java Virtual Machine
  • serialver – returns serial version UID for specified classes

4.5. Java IDL and RMI-IIOP Tools

4.5 Java IDL和RMI-IIOP工具

Java Interface Definition Language (IDL) adds Common Object-Based Request Broker Architecture (CORBA) capability to the Java platform.

Java接口定义语言(IDL)为Java平台增加了基于对象的请求代理架构(CORBA)的能力。

These tools enable distributed Java web applications to invoke operations on remote network services using industry standard Object Management Group (OMG) – IDL.

这些工具使分布式Java网络应用程序能够使用行业标准的对象管理小组(OMG)–IDL对远程网络服务进行调用操作。

Likewise, we could use Internet InterORB Protocol (IIOP).

同样地,我们可以使用互联网InterORB协议(IIOP)。

RMI-IIOP, i.e. RMI over IIOP enables programming of CORBA servers and applications via the RMI API. Thus enabling connection between two applications written in any CORBA-compliant language via Internet InterORB Protocol (IIOP).

RMI-IIOP,即 RMI over IIOP,通过 RMI API 实现 CORBA 服务器和应用程序的编程。因此,通过互联网InterORB协议(IIOP),使两个用任何CORBA兼容的语言编写的应用程序之间能够连接。

These tools include:

这些工具包括。

  • tnameserv – transient Naming Service which provides a tree-structured directory for object references
  • idlj – the IDL-to-Java Compiler for generating the Java bindings for a specified IDL file
  • orbd – enable clients to transparently locate and invoke persistent objects on the server in CORBA environment
  • servertool – provides command-line interface to register or unregister a persistent server with ORB Daemon (orbd), start and shut down a persistent server registered with ORB Daemon, etcetera

4.6. Java Deployment Tools

4.6.Java 部署工具

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

这些工具有助于在网络上部署Java应用程序和小程序。它们包括。

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

4.7. Java Plug-in Tool

4.7 Java插件工具

JDK provides us with htmlconverter. Furthermore, it’s used in conjunction with the Java Plug-in.

JDK为我们提供了htmlconverter。此外,它还与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.

一方面,Java插件在流行的浏览器和Java平台之间建立了一个连接。作为这种连接的结果,网站上的小程序可以在浏览器中运行。

On the other hand, htmlconverter is a utility for converting an HTML page containing applets to a format for Java Plug-in.

另一方面,htmlconverter是一个将包含小程序的HTML页面转换为Java Plug-in格式的工具。

4.8. Java Web Start Tool

4.8 Java Web Start工具

JDK brings javaws. We can use it in conjunction with the Java Web Start.

JDK带来了javaws。我们可以把它与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.

该工具允许我们从浏览器中单击下载并启动Java应用程序。因此,不需要运行任何安装过程。

4.9. Monitoring and Management Tools

4.9.监测和管理手段

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

这些都是很好的工具,我们可以用来监控JVM的性能和资源消耗。下面是其中的几个。:

  • 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

4.10. Troubleshooting Tools

4.10.故障排除工具

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

5. Conclusion

5.结论

In this article, we identified that the basic difference between JVM, JRE, and JDK lies in their usage.

在这篇文章中,我们确定了JVM、JRE和JDK的基本区别在于它们的用法。

First, we described how the JVM is an abstract computing machine that actually executes the Java bytecode.

首先,我们描述了JVM是如何实际执行Java字节码的抽象计算机。

Then, we explained how to just run Java applications, we use the JRE.

然后,我们解释了如何只是运行Java应用程序,我们使用JRE。

And finally, we understood how to develop Java applications, we use the JDK.

最后,我们了解了如何开发Java应用程序,我们使用JDK。

We also took some time to dig into tools and fundamental concepts of this components.

我们还花了一些时间来挖掘这个组件的工具和基本概念。