(6) The Loadmainclass in JAVA.C
Location jdk/src/share/bin/java.c
The method is responsible for loading the class where the main function resides. The method first loads the Sun.launcher.LauncherHelper class, and then calls the class's checkandloadmain to find the class where the main function resides, with the relevant code as follows.
Jclass CLS = Getlauncherhelperclass (env);//This function loads the Sun.launcher.LauncherHelper class null_check0 (CLS); if (Jli_istracelauncher ()) { start = Counterget (); } Null_check0 (mid = (*env)->getstaticmethodid (env, cls,//Find method ID "Checkandloadmain", "(ziljava/lang/ (String;) ljava/lang/class; ")); str = newplatformstring (env, name); result = (*env)->callstaticobjectmethod (env, CLS, Mid, Use_stderr, mode, str);//Call this static method
(7) The Checkandloadmain in Launcherhelper.java
Location: Jdk/src/share/bin/classes/sun/launcher/launcherhelper.java
Java code is more intuitive, I write the comments directly
public static Class<?> Checkandloadmain (Boolean printtostderr, int mode, String-what) { initoutput ( PRINTTOSTDERR); Get the class name String cn = null; <pre name= "code" class= "java" style= "FONT-SIZE:17.7777786254883PX; line-he ight:26px; " >//here to determine if mode is an incoming class or jar, corresponding to the command line Java-jar Xxx.jar or Java xxx.class
Switch (mode) {Case LM_CLASS:CN = what, break, case LM_JAR:CN = Getmainclassfromjar;//This function is implemented from the Mainfest file of the JAR to get the main The name of the class break; Default://Should never happen throw new Internalerror ("" + Mode + ": Unknown launch Mode");} cn = Cn.replace ('/', '. '); Class<?> mainClass = null; try {mainClass = Scloader.loadclass (CN);//delegate to the ClassLoader to load the main class, the loader water is too deep, does not expand here said} catch (Noclassdeffounderror | ClassNotFoundException cnfe) {if (System.getproperty ("Os.name", ""). Contains ("Os X") && Normalizer.isnormalized (CN, Normalizer.Form.NFD)) {try {//on MAC OS X since all names with diacretic symbols is given As decomposed it//is possible this main class name comes incorrectly from the command line//And we had to Re-compose It mainClass = Scloader.loadclass (Normalizer.normalize (CN, Normalizer.Form.NFC)); } catch (Noclassdeffounderror | ClassNotFoundException cnfe1) {abort (CNFE, "Java.launcher.cls.error1", CN);}} else {abort (CNFE, "Java.launcher.cls.error1", CN);}} Set to MaiNClass appclass = MainClass; /* * Check if Fxhelper can launch it using the FX launcher. In an FX app, * The main class could or May is not having a main method, so does this before * validating the main class. */if (mainclass.equals (fxhelper.class) | | Fxhelper.doesextendfxapplication (MainClass)) {//would abort () if there is problems with the FX runtime Fxhelper.setfxlau Nchparameters (what, mode); return fxhelper.class; } validatemainclass (MainClass);//validation is done here, and the validation function is analyzed later. return mainClass; }
The code that verifies that the main class is valid is then given.
static void Validatemainclass (Class<?> mainClass) { Method mainmethod; try { Mainmethod = Mainclass.getmethod ("main", string[].class);//See if there is a method for signing the following: Main (string[] Argvs) } catch ( Nosuchmethodexception nsme) { //invalid main or not FX application, abort with an error abort (null, "Java.launche R.cls.error4 ", Mainclass.getname (), fxhelper.javafx_application_class_name); Return Avoid compiler issues } int mod = Mainmethod.getmodifiers ();//Get qualifier if (! Modifier.isstatic (MoD)) {//If it is not static then the error is abort (null, "Java.launcher.cls.error2", "Static", Mainmethod.getdeclaringclass (). GetName ()); } if (Mainmethod.getreturntype ()! = Java.lang.Void.TYPE) { abort (null, "Java.launcher.cls.error3",//error if not Void) Mainmethod.getdeclaringclass (). GetName ()); } }
Second, summary
These two blogs mainly analyze what happens when you execute a Java command, and read the code to see how the configured environment variables work, when the JVM initializes, and the main class loading and validation process.
There are at least the following points in this blog not in-depth analysis to:
How the JVM is initialized.
The relationship and implementation of Env structure and VM structure.
How Scloader works.
Launcherhelper how to load.
These questions may be analyzed later in the blog.
What happens when you execute Java-jar Somefile.jar (ii)