Understand the compilation and operation of JDK, JRE, JVM and Java programs


Preface

  For Java beginners, you need to have a certain understanding of JDK, JRE, and JVM. Before we write the first Java program, we also need to have a simple understanding of Java loading and execution.

1. The relationship between JDK, JRE, and JVM

Insert picture description here

  1. JDK (Java Development Kit) Java Development Kit
    It is a must be installed by every Java software developer. After the JDK is installed, it will come with a JRE.

  2. JRE (Java Runtime Environment) Java Runtime Environment The
    set of environments necessary to run java programs, including JVM standard implementation and Java core class libraries. If you only need to run java programs, you don't need to install JDK, just install JRE.

  3. JVM (Java Virtual Machine) Java Virtual Machine
    JVM is a virtual computer that is a magic weapon to realize the cross-platform nature of java programs.
    Insert picture description here

Two, the two stages of a Java program-compile and run

  This is how a Java program goes from development to final operation.
Insert picture description here

  • Write code to
      create a .java file, and write source code that conforms to java syntax in the file . This file is called a source file .

  • Compilation
      Use the javac command to compile the source file to generate a .class file. This file is called a bytecode file . The content of the file is not a binary file. If it is a binary file, the operating system can directly execute it.

  • Run
      Use the java command to run the successfully compiled java program. The JVM is started, and the .class bytecode file is found from the hard disk through the "ClassLoader" and loaded. The JVM interpreter interprets the bytecode and finally interprets it as a binary code , and then the operating system communicates with the hardware platform by executing binary instructions Complete the interaction.

      The above is the entire process of java program loading and execution. Among them, it is important to emphasize:

  1. From development to running, Java programs include two stages : compilation and running . These two stages can be completed in different operating systems, such as compiling under windows and running under liunx system. This is because of the existence of the JVM mechanism. It can be compiled once and run everywhere , which is what we call cross-platform and portability .

  2. The compile stage needs to use the javac.exe command, which is a command that exists after the JDK is installed, and the run stage needs to use java.exe , which is a command that exists after the JRE is installed.

  3. The .class bytecode file is the final program to be executed, and the deletion of the .java source file does not affect the running of the java program.

  4. The prerequisite for running the java program is that the corresponding version of the JVM is installed in the current operating system. The JVM is not installed separately, just install the JRE. Different operating systems need to install different JRE, and different versions of JRE correspond to different versions of JVM.

to sum up

  Understand the relationship between JDK, JRE, and JVM, master the entire process of java program from compiling to running, and understand the mechanism of JVM.

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/109908335