Difference between JRE and JDK

JRE (Java Runtime Enviroment) is a runtime environment for Java. For users of Java programs, not developers. If you only downloaded and installed the JRE, your system can only run Java programs. JRE is a collection of environments necessary to run Java programs, including the JVM standard implementation and the Java core class library. It includes the Java virtual machine, Java platform core classes, and supporting files. It does not include development tools (compiler, debugger, etc.).
JDK (Java Development Kit), also known as J2SDK (Java2 Software Development Kit), is a Java development kit that provides a Java development environment (provides tools such as the compiler javac for compiling java files to class files) and runs Environment (provides JVM and Runtime helper packages for parsing class files to get them running). If you download and install the JDK, you can not only develop Java programs, but also have a platform for running Java programs. The JDK is the core of the entire Java, including the Java Runtime Environment (JRE), a bunch of Java tools tools.jar, and the Java Standard Class Library (rt.jar).
2. Difference
The JRE mainly includes: the class files of the java class library (all packaged into a jar in the lib directory) and the virtual machine (jvm.dll); the JDK mainly includes: the class files of the java class library (all packaged into a jar in the lib directory) ) And comes with a JRE. So why does the JDK come with a JRE? And jdk / jre / bin contains jvm.dll in both client and server folders (indicating that the JRE that comes with the JDK has two virtual machines).
Remember to set the jdk / bin path in the environment variable path? The teacher will tell you that javac and java will not work if you don't set it. Indeed, the jdk / bin directory contains all the commands. But does anyone think that the java command we use is not in the jdk / bin directory but in the jre / bin directory? Do not believe that you can do an experiment. You can cut the java.exe in the jdk / bin directory to another place and run the java program. What did you find? Everything is OK! (There is no javac command in JRE, the reason is simple, it is not a development environment.) So someone would ask? I obviously did not set the jre / bin directory to the environment variable? Imagine that if java is provided for most people, they don't need jdk for development. They only need jre to make java programs run. So how much trouble is it for each customer to manually set environment variables? So when you install jre, the installer automatically helps you add jre's java.exe to the system variables. The verification method is very simple. Go to Windows / system32 and take a look. What did you find? There is a java.exe.
Difficult point
If the JDK is installed, you will find that your computer has two sets of JRE, one is located in C: \ Program Files \ Java \ jre6, and the other is located in C: \ Program Files \ Java \ jdk1.6.0_41 \ jre directory.
The status of the JRE is like a PC. The Win32 applications we write need the operating system to run for us. Similarly, the Java programs we write must also run in the JRE. So after you install the JDK, if you have two sets of JRE installed in two different places on your hard disk, then you can imagine that your computer has two virtual Java PCs, both of which have the ability to run Java programs. So we can say that as long as JRE is installed on your computer, you can run Java applications correctly.
1. Why does Sun ask JDK to install two identical JREs?
This is because there are many development tools written in Java in the JDK, such as javac.exe, jar.exe, etc. These commands are placed in the C: \ Program Files \ Java \ jdk1.6.0_41 \ bin directory.
Because they are commands written in java, they rely on java jar packages, which are stored in the C: \ Program Files \ Java \ jdk1.6.0_41 \ lib directory. If you rename tools.jar in the C: \ Program Files \ Java \ jdk1.6.0_41 \ lib \ directory to tools1.jar, and then run javac.exe, the following results are displayed:
Exception in thread "main" java.lang.NoClassDefFoundError: com / sun / tools / javac / Main
But enter: java -cp C: \ Program Files \ Java \ jdk1.6.0_41 \ lib \ tools1.jar com.sun.tools.javac.Mainx
You get the same results as javac.exe.
From here we can prove that javac.exe is just a wrapper (Wrapper), and the purpose is to save developers from typing too long.
And we can find that the programs in the C: \ Program Files \ Java \ jdk1.6.0_41 \ bin directory are all very small, not larger than 29K. From here we can draw a conclusion. Even the tools in the JDK are almost written in Java, so they are also Java applications. Therefore, to use the tools attached to the JDK to develop Java programs, you must also attach a set of JREs, so they are located in C: \ Program Files \ The set of JREs in the Java \ jdk1.6.0_41 \ jre directory is used to run general Java programs.
2. If two or more JREs are installed on a computer, who will decide?
This big task lies with java.exe. The job of java.exe is to find the right JRE to run a Java program. java.exe looks for JREs in the following order: Is there a JRE in its own directory; Is there a JRE in the parent directory; Query the registry:
[HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft \ Java Runtime Environment]
So the result of running java.exe has a lot to do with which JRE is executed on your computer.