What happens when I perform a java-jar Somefile.jar (i)

Source: Internet
Author: User


Recently read the JVM source code, some of the experience as a blog to share, so they opened a new topic.


The first article named when I was very confused, my code reading is starting from launcher, that is, Java.exe (if it is the Windows platform) corresponding to the relevant code, but I can not take "JVM startup process Analysis" and other names, Because from the perspective of the analysis of the main process is still not deep to this level, so a strange name for the moment.


If this series can continue, without special instructions, the JDK and JVM versions used are 8u20, from openjdk:http://hg.openjdk.java.net/jdk8u


I am a Java programmer, in the analysis of the JVM a large number of C + +, there will inevitably be inappropriate places, but also hope that the readers correct. Between the time and the code "Long", the specific implementation details can not be exhaustive, only focus on analysis and understand the general process and mechanism, if there are more important details are missing, welcome message discussion.


In this series, any description of the source file location uses a relative path, where jdk/represents the root directory where the JDK source code is placed, and hotspot/represents the root directory where the JVM source code is placed.


First, launcher code analysis

(1) Main in MAIN.C

Location: JDK/SRC/BIN/MAIN.C

When we call the Java command, the first step is to enter the main function of C/s + +, just like the HelloWorld I wrote a few years ago.

This main function is located in the JDK/SRC/BIN/MAIN.C, which is placed in the JDK8 in the previous older version of the JVM-related code.

There is virtually no logic in main.c, mainly copying some parameters, processing some calls in the Windows platform, and then passing parameters to the Jli_launch for execution. The relevant code is on line 125th.

Return Jli_launch (MARGC, MARGV,                   sizeof (Const_jargs)/sizeof (char *), Const_jargs,                   sizeof (Const_appclasspath )/sizeof (char *), Const_appclasspath,                   full_version,                   dot_version,                   (const_progname! = NULL)? const_ Progname: *MARGV,                   (const_launcher! = null)? Const_launcher: *MARGV,                   (Const_jargs! = null)? Jni_true:jni_false,                   Const_cpwildcard, CONST_JAVAW, Const_ergo_class);



(2) The Jli_launch in JAVA.C

Location: JDK/SRC/BIN/JAVA.C

In JAVA.C, it is possible to see the meaning of each parameter of the function according to the annotations:

Jli_launch (int argc, char * * argv,/              * main ARGC, ARGC */        int JARGC, const char** JARGV,/          * java args */in        T APPCLASSC, const char** APPCLASSV,/  * app Classpath */        const char* fullversion,/                * full version defined */
   const char* dotversion,/                 * DOT version defined */        const char* pname,/                      * Program name */        const char* L Name,/                      * Launcher name */        Jboolean Javaargs,/                      * Java_args */        Jboolean Cpwildcard,/                    * Classpath wildcard*/        Jboolean JAVAW,/                         * windows-only JAVAW *        jint ergo/*                               Ergonomics class Policy * /

In line No. 236, call the system function to get the environment variables, assigning values to Jrepath,jvmpath and jvmcfg. (The environment variables set in each Java Basic tutorial work here)

    Createexecutionenvironment (&ARGC, &ARGV,                               jrepath, sizeof (Jrepath),                               Jvmpath, sizeof (Jvmpath),                               Jvmcfg,  sizeof (JVMCFG));

The No. 248 line loads Jvm.dll This file, just loads the file into memory, does nothing, and assigns a value to the IFN struct (based on the DLL extract function call address Assignment), the IFN struct contains three key function pointers.

if (! LOADJAVAVM (Jvmpath, &IFN)) {        return (6);    }
The IFN structure is as follows:

typedef struct {    createjavavm_t createjavavm;    getdefaultjavavminitargs_t Getdefaultjavavminitargs;    getcreatedjavavms_t Getcreatedjavavms;} Invocationfunctions;
According to the function name can probably guess, the first is to create a virtual machine, the second is to get the initial parameters, the third is to create a lot of virtual machines? (VMs represent the plural form of virtual machines?) I guess. )。

Some additional processing of the parameters (including getting classpath, printing debug information, adding additional parameters, etc.), and calling Jvminit to initialize the virtual machine on line 299

    Return Jvminit (&IFN, Threadstacksize, argc, argv, mode, what, ret);


(3) The Jvminit in JAVA.C

Location:jdk/src/bin/java.c

The code is very small, print the information first, and then call the Conitueinnewthread function, as you can see from the function name, a new thread is enabled to build the JVM.

Intjvminit (invocationfunctions* IFN, Jlong threadstacksize,        int argc, char **argv,        int mode, char *what, int ret) {    showsplashscreen ();    Return Continueinnewthread (IFN, Threadstacksize, argc, argv, mode, what, ret);}


(4) The Continueinnewthread in JAVA.C

Location: JDK/SRC/BIN/JAVA.C

This function still does not do anything, is to encapsulate the parameters, and then delegated to ContinueInNewThread0. Instead, ContinueInNewThread0 opens a new thread and executes the Javamain function.


(5) The Javamain in JAVA.C

Location: JDK/SRC/BIN/JAVA.C

First, the JVM is initialized in line No. 371, and the INITIALIZEJVM function is called IFN->CREATEJAVAVM, and the specific JVM boot process is too deep to analyze here. If the initialization succeeds, the VM object and the Env object are assigned values, and Env is a very important object that will be used frequently when making JNI calls.

if (! INITIALIZEJVM (&VM, &env, &IFN)) {        jli_reporterrormessage (jvm_error1);        Exit (1);    }
Then check if the jar file or a class name is passed in, and if not, print the usage

Then the No. 439 row gets the main class, and the way to get the main class is interesting, and it will be analyzed later. Then see if you throw an exception, and if you throw an exception, exit directly.

    MainClass = Loadmainclass (env, mode, what);    Check_exception_null_leave (MainClass);


Then load something for JavaFX (if needed), then get the ID of this method, combine the parameters, and invoke it.

Each of these steps checks for an exception to be thrown.

Mainid = (*env)->getstaticmethodid (env, MainClass, "main",                                       "([ljava/lang/string;) V");    Check_exception_null_leave (Mainid);    /* Build Platform specific argument array *    /Mainargs = Createapplicationargs (env, argv, argc);    Check_exception_null_leave (Mainargs);    /* Invoke Main method. *    /(*ENV)->callstaticvoidmethod (env, MainClass, Mainid, Mainargs)    ;     /* * The Launcher's exit code (in the absence of calls to     * system.exit) would be non-zero if main threw an excepti On.     *    /ret = (*env)->exceptionoccurred (env) = = NULL? 0:1;



(not to be continued)






What happens when I perform a java-jar Somefile.jar (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.