1. Definition

JRE(Java Runtime Enviroment)It is the runtime environment for Java. It is aimed at users of Java programs, not developers. If you only download and install the JRE, then your system can only run Java programs. The JRE is a collection of environments necessary for running Java programs, including the standard implementation of the JVM and the Java core class libraries. It includes the Java Virtual Machine, the core classes of the Java platform, 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. It provides the Java development environment (providing tools such as the compiler javac, used to compile java files into class files) and the runtime environment (providing the JVM and Runtime auxiliary packages, used to parse class files so they can run). If you download and install the JDK, then you can not only develop Java programs, but also have the platform to run Java programs. The JDK is the core of the entire Java, including the Java Runtime Environment (JRE), a set of Java tools tools.jar, and the Java standard class library (rt.jar).

2. Difference

The JRE mainly contains: the class files of the Java class library (all packaged as jars in the lib directory) and the virtual machine (jvm.dll); the JDK mainly contains: the class files of the Java class library (all packaged as jars in the lib directory) and comes with its own JRE. So why does the JDK come with its own JRE? Moreover, both the client and server folders under jdk/jre/bin contain jvm.dll (indicating that the JRE that comes with the JDK has two virtual machines).

Remember setting the jdk/bin path in the environment variable path?The teacher will tell everyone that without setting it, javac and java cannot be used. Indeed, the jdk/bin directory contains all the commands. But has anyone thought that the java command we use is not from the jdk/bin directory but from the jre/bin directory? If you don't believe it, you can do an experiment: you can cut the java.exe in the jdk/bin directory to another place and run a Java program again. What did you find? Everything is OK! (There is no javac command in the JRE, the reason is simple: it is not a development environment.) Then someone may ask: I obviously did not set the jre/bin directory in the environment variable? Think about it: if Java is to be provided for use by most people, they do not need the JDK for development; they only need the JRE to make Java programs run. Then every customer would still need to manually set environment variables — how troublesome! So when installing the JRE, the installer automatically adds the 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.

3. Difficulties

If you install the JDK, you will find that your computer has two sets of JRE, one located at C:\Program Files\Java\jre6, and the other located atC:\Program Files\Java\jdk1.6.0_41\jre directory.

The status of the JRE is like a PC. Our written Win32 applications need the operating system to help us run them; similarly, our written Java programs also need the JRE to run. So after you install the JDK, if two sets of JRE are installed in two different places on the hard disk, then you can imagine that your computer has two virtual Java PCs, both with the ability to run Java programs. So we can say that as long as your computer has the JRE installed, it can correctly run Java applications.

1. Why does Sun have the JDK install two identical JREs?

This is because the JDK contains many development tools written in Java, 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 need to rely on Java's jar packages. These jar packages are stored inC:\Program Files\Java\jdk1.6.0_41\libdirectory.

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 result is displayed:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac /Main 

But if you enter:

java -cp C:\Program Files\Java\jdk1.6.0_41\lib\tools1.jar com.sun.tools.javac.Mainx

You will get the same result as javac.exe.

From this we can prove that javac.exe is just a wrapper (Wrapper), and it is made to save developers from entering too long commands.

Moreover, we can find that the programs in the C:\Program Files\Java\jdk1.6.0_41\bin directory are all very small, no larger than 29K. From this we can draw a conclusion: the tools in the JDK are almost all written in Java, so they are also Java applications. Therefore, to use the tools included with the JDK to develop Java programs, you must also include a JRE. So the JRE in the C:\Program Files\Java\jdk1.6.0_41\jre directory is used for running general Java programs.

2. If a computer has two or more JREs installed, who decides?

This important task falls on java.exe. java.exe's job is to find the appropriate JRE to run a Java program. java.exe searches for the JRE in the following order: whether there is a JRE in its own directory; whether there is a JRE in the parent directory; querying the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment] 

Therefore, the running result of java.exe has a lot to do with which JRE in your computer is executed.

Article source: http://swiftlet.net/archives/639