In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how the main method is implemented in the Java language". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how the main method is implemented in the Java language".
For Java, the bottom layer is the Java virtual machine running, that is, JVM. If there is no special note, this article defaults to Hotspot as the research object.
Let's review that article first. For the C _ main + program, it mainly goes through four stages from the creation process to the entry of the CAccord function:
Process & main thread creation phase
The main thread starts execution and performs process-level initialization operations (such as loading system dynamic link libraries)
The main thread enters the entry to the executable file (OEP) and initializes the C _ CPU + runtime library.
Call in the main function from the Candlespace + runtime library
You know, Java's virtual machine JVM is mainly written by C++, so JVM is essentially a C++ program.
Therefore, the above four phases also apply to JVM.
It's just that, for the Java Candle + program, the main function has been entered at this point, and the topic can be over, while for the Java program, the execution to the main of JVM is just the beginning.
Main of JVM
The story starts with the main function of JVM
You should know that whether you are an ordinary Java program, or use Spring or any other framework, the final program runs in a Java process, and the executable file for this process is an exe (on windows) or elf (on linux).
Let's start with this executable file, take the Java8 version on the Linux system as an example, and open it with the disassembly artifact IDA to see the entry to the executable file:
Consistent with the process we analyzed in the previous article, after entering the startup entry of this program, we will go through a series of calls and finally come to the main function:
Disassembly looks good, but fortunately, there is an open source version of the HotSpot virtual machine, so we can go to OpenJDK to find the source code of this main function.
There is still a big difference between different versions. Take Java8 as an example:
Code path: https://github.com/openjdk/jdk/blob/jdk8-b20/jdk/src/share/bin/main.c
In this code, in addition to the main function, you can also see that if the JAVAW macro definition is defined, the entry has changed from main to WinMain function. Friends who have developed Windows applications should smile with satisfaction at this time.
If JAVAW is defined, it is a Win32 GUI program, and of course there is no macro definition on Linux, but this is not the subject of this article.
You can see that the main function is just a wrapper and goes directly into JLI_Launch.
This function is located in the next java.c file in the same directory. It is a very important initialization function for JVM. It mainly completes the following things:
Parameter parsing, environment configuration
Check the Java operating environment
Load JVM core dynamic library libjvm.so
Create and initialize the Java virtual machine object
None of these processes is the goal of this article, and we continue to focus on how the main function in Java is called.
At the end of JLI_Launch, ContinueInNewThread is called, and we can also see what it does from the name of this function.
This function is also a layer of encapsulation, and the real working function ContinueInNewThread0 is called internally:
The next step is to create a thread to continue later, but creating a thread involves a call to the operating system API, so this function has a corresponding implementation in different versions of the system. Looking at the first parameter passed to it, this is the entry function that will be executed when the new thread starts: JavaMain.
JavaMain
The name of this function is a little interesting. It seems that it is about to enter the realm of Java. Come on and read on:
Int JNICALL JavaMain (void * _ args) {/ / find the startup class mainClass = LoadMainClass (env, mode, what); / / find the main function in the startup class mainID = (* env)-> GetStaticMethodID (env, mainClass, "main", "([Ljava/lang/String;) V") / /... / call it (* env)-> CallStaticVoidMethod (env, mainClass, mainID, mainArgs); / /.}
There are a lot of details in JavaMain, and we extract what we need to care about. Calling the main method we wrote is like putting an elephant in the refrigerator. There are three steps:
Find the startup class
Find the main method in the startup class
Call it
The specific search process will not be expanded here, it is a bit tedious, but you should be able to guess that the compiled Java code is stored in the form of class files, so this search involves a series of work such as class class loading.
In short, an operation is as fierce as a tiger. Hey, JVM found the main method we wrote! The next step is to call it.
Enter the Java world
The main method is called by CallStaticVoidMethod, which, as you can see from the name, is calling a static method with an empty return value. Attention, C++ 's territory is almost to the border, we are about to pass it to the beautiful new world of Java!
The inner part of this function will come to:
JavaCalls::call (result, method, & java_args, CHECK)
Eventually, the Java method stack frame is created, the template interpreter is ready, and then the bytecode is executed at the interpreter entry, officially entering the Java world!
The first stop in the Java world is the main method of the startup class found earlier, where you start the program's journey in the Java world.
Thank you for reading, the above is the content of "how the main method is implemented in the Java language". After the study of this article, I believe you have a deeper understanding of how the main method is implemented in the Java language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.