< prev index next >

src/java.base/share/native/libjli/java.c

Print this page




 108 static void SetPatchProp(const jint n, const char *s);
 109 static void SelectVersion(int argc, char **argv, char **main_class);
 110 static void SetJvmEnvironment(int argc, char **argv);
 111 static jboolean ParseArguments(int *pargc, char ***pargv,
 112                                int *pmode, char **pwhat,
 113                                int *pret, const char *jrepath);
 114 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
 115                               InvocationFunctions *ifn);
 116 static jstring NewPlatformString(JNIEnv *env, char *s);
 117 static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
 118 static jclass GetApplicationClass(JNIEnv *env);
 119 
 120 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
 121 static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
 122 static void SetApplicationClassPath(const char**);
 123 
 124 static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
 125 static void PrintUsage(JNIEnv* env, jboolean doXUsage);
 126 static void ShowSettings(JNIEnv* env, char *optString);
 127 static void ListModules(JNIEnv* env, char *optString);

 128 
 129 static void SetPaths(int argc, char **argv);
 130 
 131 static void DumpState();
 132 static jboolean RemovableOption(char *option);
 133 
 134 /* Maximum supported entries from jvm.cfg. */
 135 #define INIT_MAX_KNOWN_VMS      10
 136 
 137 /* Values for vmdesc.flag */
 138 enum vmdesc_flag {
 139     VM_UNKNOWN = -1,
 140     VM_KNOWN,
 141     VM_ALIASED_TO,
 142     VM_WARN,
 143     VM_ERROR,
 144     VM_IF_SERVER_CLASS,
 145     VM_IGNORE
 146 };
 147 


 308     /* set the -Dsun.java.launcher.* platform properties */
 309     SetJavaLauncherPlatformProps();
 310 
 311     return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
 312 }
 313 /*
 314  * Always detach the main thread so that it appears to have ended when
 315  * the application's main method exits.  This will invoke the
 316  * uncaught exception handler machinery if main threw an
 317  * exception.  An uncaught exception handler cannot change the
 318  * launcher's return code except by calling System.exit.
 319  *
 320  * Wait for all non-daemon threads to end, then destroy the VM.
 321  * This will actually create a trivial new Java waiter thread
 322  * named "DestroyJavaVM", but this will be seen as a different
 323  * thread from the one that executed main, even though they are
 324  * the same C thread.  This allows mainThread.join() and
 325  * mainThread.isAlive() to work as expected.
 326  */
 327 #define LEAVE() \

 328     do { \
 329         if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
 330             JLI_ReportErrorMessage(JVM_ERROR2); \
 331             ret = 1; \
 332         } \
 333         if (JNI_TRUE) { \
 334             (*vm)->DestroyJavaVM(vm); \
 335             return ret; \
 336         } \
 337     } while (JNI_FALSE)
 338 
 339 #define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
 340     do { \
 341         if ((*env)->ExceptionOccurred(env)) { \
 342             JLI_ReportExceptionDescription(env); \
 343             LEAVE(); \
 344         } \
 345         if ((CENL_exception) == NULL) { \
 346             JLI_ReportErrorMessage(JNI_ERROR); \
 347             LEAVE(); \


 369     InvocationFunctions ifn = args->ifn;
 370 
 371     JavaVM *vm = 0;
 372     JNIEnv *env = 0;
 373     jclass mainClass = NULL;
 374     jclass appClass = NULL; // actual application class being launched
 375     jmethodID mainID;
 376     jobjectArray mainArgs;
 377     int ret = 0;
 378     jlong start, end;
 379 
 380     RegisterThread();
 381 
 382     /* Initialize the virtual machine */
 383     start = CounterGet();
 384     if (!InitializeJVM(&vm, &env, &ifn)) {
 385         JLI_ReportErrorMessage(JVM_ERROR1);
 386         exit(1);
 387     }
 388 


 389     if (showSettings != NULL) {
 390         ShowSettings(env, showSettings);
 391         CHECK_EXCEPTION_LEAVE(1);
 392     }
 393 
 394     if (listModules != NULL) {
 395         ListModules(env, listModules);
 396         CHECK_EXCEPTION_LEAVE(1);
 397         LEAVE();
 398     }
 399 
 400     if (printVersion || showVersion) {
 401         PrintJavaVersion(env, showVersion);
 402         CHECK_EXCEPTION_LEAVE(0);
 403         if (printVersion) {
 404             LEAVE();
 405         }
 406     }
 407 
 408     /* If the user specified neither a class name nor a JAR file */


1667                                  (jlong)threadStackSize,
1668                                  ServerClassMachine());
1669 }
1670 
1671 /**
1672  * List modules supported by the runtime
1673  */
1674 static void
1675 ListModules(JNIEnv *env, char *optString)
1676 {
1677     jmethodID listModulesID;
1678     jstring joptString;
1679     jclass cls = GetLauncherHelperClass(env);
1680     NULL_CHECK(cls);
1681     NULL_CHECK(listModulesID = (*env)->GetStaticMethodID(env, cls,
1682             "listModules", "(ZLjava/lang/String;)V"));
1683     NULL_CHECK(joptString = (*env)->NewStringUTF(env, optString));
1684     (*env)->CallStaticVoidMethod(env, cls, listModulesID,
1685                                  USE_STDERR,
1686                                  joptString);































1687 }
1688 
1689 /*
1690  * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
1691  */
1692 static void
1693 PrintUsage(JNIEnv* env, jboolean doXUsage)
1694 {
1695   jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1696   jstring jprogname, vm1, vm2;
1697   int i;
1698   jclass cls = GetLauncherHelperClass(env);
1699   NULL_CHECK(cls);
1700   if (doXUsage) {
1701     NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1702                                         "printXUsageMessage", "(Z)V"));
1703     (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
1704   } else {
1705     NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1706                                         "initHelpMessage", "(Ljava/lang/String;)V"));




 108 static void SetPatchProp(const jint n, const char *s);
 109 static void SelectVersion(int argc, char **argv, char **main_class);
 110 static void SetJvmEnvironment(int argc, char **argv);
 111 static jboolean ParseArguments(int *pargc, char ***pargv,
 112                                int *pmode, char **pwhat,
 113                                int *pret, const char *jrepath);
 114 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
 115                               InvocationFunctions *ifn);
 116 static jstring NewPlatformString(JNIEnv *env, char *s);
 117 static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
 118 static jclass GetApplicationClass(JNIEnv *env);
 119 
 120 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
 121 static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
 122 static void SetApplicationClassPath(const char**);
 123 
 124 static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
 125 static void PrintUsage(JNIEnv* env, jboolean doXUsage);
 126 static void ShowSettings(JNIEnv* env, char *optString);
 127 static void ListModules(JNIEnv* env, char *optString);
 128 static void SetNativeThreadName(JNIEnv* env, char *name);
 129 
 130 static void SetPaths(int argc, char **argv);
 131 
 132 static void DumpState();
 133 static jboolean RemovableOption(char *option);
 134 
 135 /* Maximum supported entries from jvm.cfg. */
 136 #define INIT_MAX_KNOWN_VMS      10
 137 
 138 /* Values for vmdesc.flag */
 139 enum vmdesc_flag {
 140     VM_UNKNOWN = -1,
 141     VM_KNOWN,
 142     VM_ALIASED_TO,
 143     VM_WARN,
 144     VM_ERROR,
 145     VM_IF_SERVER_CLASS,
 146     VM_IGNORE
 147 };
 148 


 309     /* set the -Dsun.java.launcher.* platform properties */
 310     SetJavaLauncherPlatformProps();
 311 
 312     return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
 313 }
 314 /*
 315  * Always detach the main thread so that it appears to have ended when
 316  * the application's main method exits.  This will invoke the
 317  * uncaught exception handler machinery if main threw an
 318  * exception.  An uncaught exception handler cannot change the
 319  * launcher's return code except by calling System.exit.
 320  *
 321  * Wait for all non-daemon threads to end, then destroy the VM.
 322  * This will actually create a trivial new Java waiter thread
 323  * named "DestroyJavaVM", but this will be seen as a different
 324  * thread from the one that executed main, even though they are
 325  * the same C thread.  This allows mainThread.join() and
 326  * mainThread.isAlive() to work as expected.
 327  */
 328 #define LEAVE() \
 329     SetNativeThreadName(env, "DestroyJavaVM"); \
 330     do { \
 331         if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
 332             JLI_ReportErrorMessage(JVM_ERROR2); \
 333             ret = 1; \
 334         } \
 335         if (JNI_TRUE) { \
 336             (*vm)->DestroyJavaVM(vm); \
 337             return ret; \
 338         } \
 339     } while (JNI_FALSE)
 340 
 341 #define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
 342     do { \
 343         if ((*env)->ExceptionOccurred(env)) { \
 344             JLI_ReportExceptionDescription(env); \
 345             LEAVE(); \
 346         } \
 347         if ((CENL_exception) == NULL) { \
 348             JLI_ReportErrorMessage(JNI_ERROR); \
 349             LEAVE(); \


 371     InvocationFunctions ifn = args->ifn;
 372 
 373     JavaVM *vm = 0;
 374     JNIEnv *env = 0;
 375     jclass mainClass = NULL;
 376     jclass appClass = NULL; // actual application class being launched
 377     jmethodID mainID;
 378     jobjectArray mainArgs;
 379     int ret = 0;
 380     jlong start, end;
 381 
 382     RegisterThread();
 383 
 384     /* Initialize the virtual machine */
 385     start = CounterGet();
 386     if (!InitializeJVM(&vm, &env, &ifn)) {
 387         JLI_ReportErrorMessage(JVM_ERROR1);
 388         exit(1);
 389     }
 390 
 391     SetNativeThreadName(env, "main");
 392 
 393     if (showSettings != NULL) {
 394         ShowSettings(env, showSettings);
 395         CHECK_EXCEPTION_LEAVE(1);
 396     }
 397 
 398     if (listModules != NULL) {
 399         ListModules(env, listModules);
 400         CHECK_EXCEPTION_LEAVE(1);
 401         LEAVE();
 402     }
 403 
 404     if (printVersion || showVersion) {
 405         PrintJavaVersion(env, showVersion);
 406         CHECK_EXCEPTION_LEAVE(0);
 407         if (printVersion) {
 408             LEAVE();
 409         }
 410     }
 411 
 412     /* If the user specified neither a class name nor a JAR file */


1671                                  (jlong)threadStackSize,
1672                                  ServerClassMachine());
1673 }
1674 
1675 /**
1676  * List modules supported by the runtime
1677  */
1678 static void
1679 ListModules(JNIEnv *env, char *optString)
1680 {
1681     jmethodID listModulesID;
1682     jstring joptString;
1683     jclass cls = GetLauncherHelperClass(env);
1684     NULL_CHECK(cls);
1685     NULL_CHECK(listModulesID = (*env)->GetStaticMethodID(env, cls,
1686             "listModules", "(ZLjava/lang/String;)V"));
1687     NULL_CHECK(joptString = (*env)->NewStringUTF(env, optString));
1688     (*env)->CallStaticVoidMethod(env, cls, listModulesID,
1689                                  USE_STDERR,
1690                                  joptString);
1691 }
1692 
1693 /**
1694  * Set native thread name as possible.
1695  */
1696 static void
1697 SetNativeThreadName(JNIEnv *env, char *name)
1698 {
1699     jmethodID currentThreadID, setNativeNameID;
1700     jobject currentThread;
1701     jstring nameString;
1702     jclass cls;
1703 
1704     NULL_CHECK(cls = FindBootStrapClass(env, "java/lang/Thread"));
1705     NULL_CHECK(currentThreadID = (*env)->GetStaticMethodID(env, cls,
1706                                      "currentThread", "()Ljava/lang/Thread;"));
1707     NULL_CHECK(currentThread = (*env)->CallStaticObjectMethod(env, cls,
1708                                                               currentThreadID));
1709     NULL_CHECK(setNativeNameID = (*env)->GetMethodID(env, cls,
1710                                     "setNativeName", "(Ljava/lang/String;Z)V"));
1711     NULL_CHECK(nameString = (*env)->NewStringUTF(env, name));
1712     (*env)->CallVoidMethod(env, currentThread, setNativeNameID,
1713                            nameString, JNI_TRUE);
1714 
1715     if ((*env)->ExceptionOccurred(env)) {
1716       /*
1717        * We can clear pending exception because exception at this point
1718        * is not critical.
1719        */
1720       (*env)->ExceptionClear(env);
1721     }
1722 }
1723 
1724 /*
1725  * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
1726  */
1727 static void
1728 PrintUsage(JNIEnv* env, jboolean doXUsage)
1729 {
1730   jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1731   jstring jprogname, vm1, vm2;
1732   int i;
1733   jclass cls = GetLauncherHelperClass(env);
1734   NULL_CHECK(cls);
1735   if (doXUsage) {
1736     NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1737                                         "printXUsageMessage", "(Z)V"));
1738     (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
1739   } else {
1740     NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1741                                         "initHelpMessage", "(Ljava/lang/String;)V"));


< prev index next >