Top


定義場所(file name)

jdk/src/share/bin/java.c

名前(function name)

int JNICALL
JavaMain(void * _args)
{

本体部(body)

  {- -------------------------------------------
  (1) (変数宣言など)
      ---------------------------------------- -}

        JavaMainArgs *args = (JavaMainArgs *)_args;
        int argc = args->argc;
        char **argv = args->argv;
        int mode = args->mode;
        char *what = args->what;
        InvocationFunctions ifn = args->ifn;

        JavaVM *vm = 0;
        JNIEnv *env = 0;
        jclass mainClass = NULL;
        jmethodID mainID;
        jobjectArray mainArgs;
        int ret = 0;
        jlong start, end;

  {- -------------------------------------------
  (1) InitializeJVM() を呼んで HotSpot の生成と初期化を行う.

      (失敗したら, ここで exit())
      ---------------------------------------- -}

        /* Initialize the virtual machine */
        start = CounterGet();
        if (!InitializeJVM(&vm, &env, &ifn)) {
            JLI_ReportErrorMessage(JVM_ERROR1);
            exit(1);
        }

  {- -------------------------------------------
  (1) printVersion や showVersion オプションが立っていれば, PrintJavaVersion() でバージョンを表示する   
      (printVersion の場合は, ここで exit())
      ---------------------------------------- -}

        if (printVersion || showVersion) {
            PrintJavaVersion(env, showVersion);
            CHECK_EXCEPTION_LEAVE(0);
            if (printVersion) {
                LEAVE();
            }
        }

  {- -------------------------------------------
  (1) 
      ---------------------------------------- -}

        if (showSettings != NULL) {
            ShowSettings(env, showSettings);
            CHECK_EXCEPTION_LEAVE(1);
        }

  {- -------------------------------------------
  (1) printXUsage 等のオプションが立っていれば, PrintUsage() でusageを表示する.
      (そして, ここで exit())
      ---------------------------------------- -}

        /* If the user specified neither a class name nor a JAR file */
        if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
            PrintUsage(env, printXUsage);
            CHECK_EXCEPTION_LEAVE(1);
            LEAVE();
        }

  {- -------------------------------------------
  (1) 
      ---------------------------------------- -}

        FreeKnownVMs();  /* after last possible PrintUsage() */

  {- -------------------------------------------
  (1) (トレース出力)
      ---------------------------------------- -}

        if (JLI_IsTraceLauncher()) {
            end = CounterGet();
            JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
                   (long)(jint)Counter2Micros(end-start));
        }

  {- -------------------------------------------
  (1) (トレース出力)
      ---------------------------------------- -}

        /* At this stage, argc/argv have the application's arguments */
        if (JLI_IsTraceLauncher()){
            int i;
            printf("%s is '%s'\n", launchModeNames[mode], what);
            printf("App's argc is %d\n", argc);
            for (i=0; i < argc; i++) {
                printf("    argv[%2d] = '%s'\n", i, argv[i]);
            }
        }

  {- -------------------------------------------
  (1) LoadMainClass() でメインクラスをロードする.
      ---------------------------------------- -}

        ret = 1;

        /*
         * Get the application's main class.
         *
         * See bugid 5030265.  The Main-Class name has already been parsed
         * from the manifest, but not parsed properly for UTF-8 support.
         * Hence the code here ignores the value previously extracted and
         * uses the pre-existing code to reextract the value.  This is
         * possibly an end of release cycle expedient.  However, it has
         * also been discovered that passing some character sets through
         * the environment has "strange" behavior on some variants of
         * Windows.  Hence, maybe the manifest parsing code local to the
         * launcher should never be enhanced.
         *
         * Hence, future work should either:
         *     1)   Correct the local parsing code and verify that the
         *          Main-Class attribute gets properly passed through
         *          all environments,
         *     2)   Remove the vestages of maintaining main_class through
         *          the environment (and remove these comments).
         */
        mainClass = LoadMainClass(env, mode, what);
        CHECK_EXCEPTION_NULL_LEAVE(mainClass);

  {- -------------------------------------------
  (1) jni_GetStaticMethodID() により, メインクラスの main() メソッドを取得する.
      ---------------------------------------- -}

        /*
         * The LoadMainClass not only loads the main class, it will also ensure
         * that the main method's signature is correct, therefore further checking
         * is not required. The main method is invoked here so that extraneous java
         * stacks are not in the application stack trace.
         */
        mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                                           "([Ljava/lang/String;)V");
        CHECK_EXCEPTION_NULL_LEAVE(mainID);

  {- -------------------------------------------
  (1) main() メソッドに渡す引数を構築する
      ---------------------------------------- -}

        /* Build argument array */
        mainArgs = NewPlatformStringArray(env, argv, argc);
        CHECK_EXCEPTION_NULL_LEAVE(mainArgs);

  {- -------------------------------------------
  (1) jni_CallStaticVoidMethod() により, メインクラスの main() メソッドを呼び出す.
      ---------------------------------------- -}

        /* Invoke main method. */
        (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);

  {- -------------------------------------------
  (1) 返値を決定する (例外が起きていたら 1, そうでなければ 0 とする)
      ---------------------------------------- -}

        /*
         * 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;

  {- -------------------------------------------
  (1) LEAVE() マクロで後始末を行う.
      ---------------------------------------- -}

        LEAVE();
    }

This document is available under the GNU GENERAL PUBLIC LICENSE Version 2.