编者注:这个问题是在2009年提出的,世界在哪里不同。到2022年,标准的开源OpenJDK将只支持jdk。
JDK和JRE有什么区别?
它们的角色是什么,什么时候应该使用其中一个或另一个?
编者注:这个问题是在2009年提出的,世界在哪里不同。到2022年,标准的开源OpenJDK将只支持jdk。
JDK和JRE有什么区别?
它们的角色是什么,什么时候应该使用其中一个或另一个?
当前回答
通俗地说:JDK是祖父,JRE是父亲,JVM是他们的儿子。 [即JDK > JRE > JVM]
JDK = JRE +开发/调试工具 JRE = JVM + Java包类(如util, math, lang, awt,swing等)+运行时库。 JVM =类装入器系统+运行时数据区+执行引擎。
换句话说,如果你是一个Java程序员,你将需要JDK在你的系统中,这个包将包括JRE和JVM,但如果你是一个喜欢玩在线游戏的普通用户,那么你只需要JRE,这个包不会有JDK。
JVM:
The Java Virtual Machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM. It's also the entity that allows Java to be a "portable language" (write once, run anywhere). Indeed there are specific implementations of the JVM for different systems (Windows, Linux, MacOS, see the wikipedia list..), the aim is that with the same bytecodes they all give the same results.
JDK和JRE
要解释JDK和JRE之间的区别,最好是阅读Oracle文档并参考图:
Java运行环境(JRE)
The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which deploys standalone applications over a network. It is also the foundation for the technologies in the Java 2 Platform, Enterprise Edition (J2EE) for enterprise software development and deployment. The JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications.
Java开发工具包(JDK)
JDK是JRE的超集,包含JRE中的所有内容,以及开发小程序和应用程序所需的编译器和调试器等工具。
注意,Oracle并不是唯一提供JDK的公司。
JIT编译过程(来源:Oracle文档)
其他回答
JDK和JRE之间的区别在于,JDK是java的软件开发工具包,而JRE是运行程序的地方。
编译代码和将Java代码转换为字节码需要JDK,而执行字节码需要jre。
从调试的角度来看,有一个不同之处:
要调试到Java系统类,如String和ArrayList,你需要一个特殊版本的JRE,它是用“调试信息”编译的。JDK中包含的JRE提供了这些信息,但常规的JRE没有。常规JRE不包含此信息,以确保更好的性能。
什么是调试信息?以下是这篇博文的简要解释:
Modern compilers do a pretty good job converting your high-level code, with its nicely indented and nested control structures and arbitrarily typed variables into a big pile of bits called machine code (or bytecode in case of Java), the sole purpose of which is to run as fast as possible on the target CPU (virtual CPU of your JVM). Java code gets converted into several machine code instructions. Variables are shoved all over the place – into the stack, into registers, or completely optimized away. Structures and objects don’t even exist in the resulting code – they’re merely an abstraction that gets translated to hard-coded offsets into memory buffers. So how does a debugger know where to stop when you ask it to break at the entry to some function? How does it manage to find what to show you when you ask it for the value of a variable? The answer is – debugging information. Debugging information is generated by the compiler together with the machine code. It is a representation of the relationship between the executable program and the original source code. This information is encoded into a pre-defined format and stored alongside the machine code. Many such formats were invented over the years for different platforms and executable files.
简单:
JVM是执行Java代码的虚拟机
JRE是运行Java应用程序所需的环境(标准库和JVM)
JDK是带有开发人员工具和文档的JRE
从官方java网站…
JRE (Java运行环境):
它是实际执行Java程序的Java虚拟机*的实现。 Java运行时环境是运行Java程序所需的插件。 JRE比JDK小,所以它需要更少的磁盘空间。 JRE可从 https://www.java.com 它包括JVM、核心库和其他附加组件,用于运行用Java编写的应用程序和小程序。
JDK (Java Development Kit)
它是一组软件,可以用来开发基于Java的应用程序。 开发Java应用程序需要Java开发工具包。 JDK需要更多的磁盘空间,因为它包含JRE和各种开发工具。 JDK可以免费下载/支持 https://www.oracle.com/technetwork/java/javase/downloads/ 它包括JRE、一组API类、Java编译器、Webstart和编写Java小程序和应用程序所需的附加文件。