jdk 与jre

1. Definition
JRE (Java Runtime Environment) is the runtime environment of Java. For users of Java programs, not developers. If you only downloaded and installed the JRE, then your system can only run Java programs. JRE is a collection of environments necessary to run Java programs, including JVM standard implementations and Java core class libraries. It includes the Java Virtual Machine, Java Platform Core classes and supporting files. It does not contain development tools (compiler, debugger, etc.).
JDK (Java Development Kit), also known as J2SDK (Java2 Software Development Kit), is a Java development kit, which provides a Java development environment (provides tools such as compiler javac to compile java files into class files) and runs Environment (provides JVM and Runtime auxiliary packages for parsing class files to make them run). If you download and install the JDK, then you can not only develop Java programs, but also have a platform to run Java programs. JDK is the core of the entire Java, including the Java Runtime Environment (JRE), a bunch of Java tools tools.jar and Java standard library (rt.jar).
2. Difference
JRE mainly includes: class files of java class library (all packaged into jar in lib directory) and virtual machine (jvm.dll); JDK mainly includes: class file of java class library (all packaged in lib directory) jar) and comes with a JRE. So why does the JDK come with a JRE? Moreover, the client and server folders under jdk/jre/bin both contain jvm.dll (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 if you don't set it up, javac and java will not work. Indeed jdk/bin directory contains all the commands. But has anyone thought that the java command we use is not in the jdk/bin directory but in the jre/bin directory? If you don't believe me, 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 the JRE for the simple reason that it is not a development environment) So someone will ask? I obviously did not set the jre/bin directory to the environment variable? Just imagine if java is available to most people, they don't need jdk for development, they only need jre to make java programs run, then how troublesome each customer needs to manually set environment variables? So when you install jre, the installer automatically adds the java.exe of jre to the system variables for you. The verification method is very simple. Go to Windows/system32 and take a look. What did you find? There is a java.exe.
3. Difficulties
If JDK is installed, you will find that your computer has two sets of JREs, 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 Down.
The status of JRE is like a PC. Win32 applications we write need the operating system to help us run them. Similarly, the Java programs we write also need JRE to run. So after you install the JDK, if you install two sets of JRE in two different places on the hard disk, then you can imagine that your computer has two virtual Java 
PCs, both of which have the function of running Java programs. So we can say that as long as your computer has JRE installed, you can run Java applications correctly. 
1. Why does Sun let the JDK install two sets of the same JRE?
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 must rely on java jar packages, which are stored in the C:\Program Files\Java\jdk1.6.0_41\lib directory.
If C:\Program Files\Java\jdk1.6.0 Rename tools.jar in _41\lib\ directory to tools1.jar, then run javac.exe, the following result is displayed: 
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac /Main 
but input java -cp C:\Program Files\Java\jdk1.6.0_41\lib\tools1.jar com.sun.tools.javac.Main 
gives the same result as javac.exe.
From here we can prove that javac.exe is just a wrapper (Wrapper), and the purpose of making is to save developers from typing too long commands.
And we can find that the programs in the C:\Program Files\Java\jdk1.6.0_41\bin directory are very small, no larger than 29K, from which we can draw a conclusion. The tools in the JDK are almost written in Java, so they are also Java applications. Therefore, if you want to use the tools attached to the JDK to develop Java programs, you must also attach a set of JRE yourself, so it is located in C:\Program Files\ The JRE in the Java\jdk1.6.0_41\jre directory is used to run general Java programs. 
2. If more than two sets of JREs are installed on a computer, who decides?
This important task falls on java.exe. The job of java.exe is to find a suitable JRE to run Java programs. java.exe searches for JRE in the following order: is there JRE in its own directory; is there JRE in the parent directory; query the registry:
[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment] 
So the running result of java.exe is the same as yours It has a lot to do with which JRE is executed in the computer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325230643&siteId=291194637