How does Java run applications

To run a Java application, the following steps are required: After the
user finishes writing the Java source code, the Java compiler (javac command) generates a .class file. After that, the .class file is loaded through the class loader, and then the application is executed.

The seemingly simple process, let's clear it step by step.

The .java file will pass the Java compiler to generate a .class file, which is a bytecode file. There is nothing to say about this step. This part is called compilation, and it is realized by javac command in cmd. The result of compilation is not to convert the content into machine language like C++ or assembly language, but into a language that can be recognized by jvm. This specific language can eventually be executed by the Java virtual machine. This is also the reason why Java can be cross-platform. Cross-platform is realized by running the application through jvm.

The compilation process is divided into the following steps:

1、词法分析:
	找到关键字的token流。比如说if、else、while、for等关键词有对应的token流。比如:do对应DO,else对应ELSE等。
2、语法分析:
	识别这些关键词是否符合要求。比如说while后面是否紧接着布尔类型的值等等。这一步会生成抽象树,具体可以学学编译原理。
3、语意分析:
	将没有构造方法的类型添加无参构造函数;对变量进行初始化;变量的值是否匹配等等。
4、生成字节码
	经过了语意分析后,形成的抽象树已经非常好了。然后将代码转换为JVM规范的字节码。JVM的架构模型是基于栈的,所有操作都是入栈和出栈进行操作的。

After the .class file is generated, the content is loaded through the class loader. The loading steps are: loading, linking, and initialization.
Load The
bytecode is found by executing the class name, and the class is generated.
The default loading has three Bootstrap ClassLoader, Extension ClassLoader, System ClassLoader.
Bootstrap ClassLoader (root loader): Responsible for loading Java core class libraries. Let's see what libraries are loaded by the loader

URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
for (URL url : urls) {
	System.out.println(url.toExternalForm());
}
file:/D:/Program%20Files%20(x86)/Java/jre/lib/resources.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/rt.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/sunrsasign.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/jsse.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/jce.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/charsets.jar
file:/D:/Program%20Files%20(x86)/Java/jre/lib/jfr.jar
file:/D:/Program%20Files%20(x86)/Java/jre/classes

This loader is implemented by JVM itself. The
Extension ClassLoader is implemented by C++ : it is used to load the class library in the jre/lib/ext directory.
System ClassLoader (system loader): generally used to load jar packages. Stored in the project directory/bin directory,
all classes can be loaded through the above three loaders. Of course we can also customize the class loader. It should be noted that not all classes are loaded at one time, but the jvm will choose to load when which class is needed.

Link
1. Verify whether the class file meets the grammatical requirements, for example, final cannot be inherited, whether the code is complete, etc. (Used to prevent developers from arbitrarily modifying the class file).
2. Initialize the class variable, for example, int a = 10 in class A; then the a variable will be assigned a value of 0 first.
3. Analyze and load other dependent classes. For example, if there is a Person class object in class A, then the Person class object will be loaded.

Initialization
Attach the correct value to the class variable. As mentioned above, the value of the a variable is 0, and now it is officially assigned the value 10.

Finally, the JVM executes the program.

Supplement: During the loading process, the JVM process has been opened, and the GC recovery mechanism has also been opened. The GC recovery mechanism is a daemon thread, which is used to manage and allocate memory in the background. The priority of the daemon thread is lower. After all the non-daemon threads are executed, the daemon threads (GC recycling threads, etc.) will stop working. Only then will the JVM process be closed.

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/51824933