int JLI_Launch(int argc, char ** argv, /* main argc, argc */ int jargc, constchar** jargv, /* java args */ int appclassc, constchar** appclassv, /* app classpath */ constchar* fullversion, /* full version defined */ constchar* dotversion, /* dot version defined */ constchar* pname, /* program name */ constchar* lname, /* launcher name */ jboolean javaargs, /* JAVA_ARGS */ jboolean cpwildcard, /* classpath wildcard */ jboolean javaw, /* windows-only javaw */ jint ergo_class /* ergnomics policy */ );
1.初始化
1 2 3 4 5 6 7 8 9 10 11
InitLauncher(javaw); //初始化启动器 DumpState(); //打印当前状态 //确保开启启动器跟踪状态 if (JLI_IsTraceLauncher()) { int i; printf("Command line args:\n"); for (i = 0; i < argc ; i++) { printf("argv[%d] = %s\n", i, argv[i]); } AddOption("-Dsun.java.launcher.diag=true", NULL); }
/* Initialize the virtual machine */ start = CounterGet(); if (!InitializeJVM(&vm, &env, &ifn)) { JLI_ReportErrorMessage(JVM_ERROR1); exit(1); }
加载主类
1
mainClass = LoadMainClass(env, mode, what);
获取Application Main Class
某些没有主方法的Java程序比如JavaFX应用,会获取Application Main Class
1 2 3 4 5 6 7
/* * In some cases when launching an application that needs a helper, e.g., a * JavaFX application with no main method, the mainClass will not be the * applications own main class but rather a helper class. To keep things * consistent in the UI we need to track and report the application main class. */ appClass = GetApplicationClass(env);
/* Invoke main method. */ (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
LEAVE函数结束线程,销毁JVM
1 2 3 4 5 6
/* * The launcher's exit code (in the absence of calls to * System.exit) will be non-zero if main threw an exception. */ ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1; LEAVE();