1 /* 2 * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Shared source for 'java' command line tool. 28 * 29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For 30 * instance, the JDK command line tools such as javac and javadoc (see 31 * makefiles for more details) are built with this program. Any arguments 32 * prefixed with '-J' will be passed directly to the 'java' command. 33 */ 34 35 /* 36 * One job of the launcher is to remove command line options which the 37 * vm does not understand and will not process. These options include 38 * options which select which style of vm is run (e.g. -client and 39 * -server) as well as options which select the data model to use. 40 * Additionally, for tools which invoke an underlying vm "-J-foo" 41 * options are turned into "-foo" options to the vm. This option 42 * filtering is handled in a number of places in the launcher, some of 43 * it in machine-dependent code. In this file, the function 44 * CheckJvmType removes vm style options and TranslateApplicationArgs 45 * removes "-J" prefixes. The CreateExecutionEnvironment function processes 46 * and removes -d<n> options. On unix, there is a possibility that the running 47 * data model may not match to the desired data model, in this case an exec is 48 * required to start the desired model. If the data models match, then 49 * ParseArguments will remove the -d<n> flags. If the data models do not match 50 * the CreateExecutionEnviroment will remove the -d<n> flags. 51 */ 52 53 54 #include <assert.h> 55 56 #include "java.h" 57 #include "jni.h" 58 59 /* 60 * A NOTE TO DEVELOPERS: For performance reasons it is important that 61 * the program image remain relatively small until after SelectVersion 62 * CreateExecutionEnvironment have finished their possibly recursive 63 * processing. Watch everything, but resist all temptations to use Java 64 * interfaces. 65 */ 66 67 #define USE_STDERR JNI_TRUE /* we usually print to stderr */ 68 #define USE_STDOUT JNI_FALSE 69 70 static jboolean printVersion = JNI_FALSE; /* print and exit */ 71 static jboolean showVersion = JNI_FALSE; /* print but continue */ 72 static jboolean printUsage = JNI_FALSE; /* print and exit*/ 73 static jboolean printTo = USE_STDERR; /* where to print version/usage */ 74 static jboolean printXUsage = JNI_FALSE; /* print and exit*/ 75 static jboolean dryRun = JNI_FALSE; /* initialize VM and exit */ 76 static char *showSettings = NULL; /* print but continue */ 77 static jboolean showResolvedModules = JNI_FALSE; 78 static jboolean listModules = JNI_FALSE; 79 static char *describeModule = NULL; 80 static jboolean validateModules = JNI_FALSE; 81 82 static const char *_program_name; 83 static const char *_launcher_name; 84 static jboolean _is_java_args = JNI_FALSE; 85 static jboolean _have_classpath = JNI_FALSE; 86 static const char *_fVersion; 87 static jboolean _wc_enabled = JNI_FALSE; 88 static jboolean dumpSharedSpaces = JNI_FALSE; /* -Xshare:dump */ 89 90 /* 91 * Entries for splash screen environment variables. 92 * putenv is performed in SelectVersion. We need 93 * them in memory until UnsetEnv, so they are made static 94 * global instead of auto local. 95 */ 96 static char* splash_file_entry = NULL; 97 static char* splash_jar_entry = NULL; 98 99 /* 100 * List of VM options to be specified when the VM is created. 101 */ 102 static JavaVMOption *options; 103 static int numOptions, maxOptions; 104 105 /* 106 * Prototypes for functions internal to launcher. 107 */ 108 static const char* GetFullVersion(); 109 static jboolean IsJavaArgs(); 110 static void SetJavaLauncherProp(); 111 static void SetClassPath(const char *s); 112 static void SetMainModule(const char *s); 113 static void SelectVersion(int argc, char **argv, char **main_class); 114 static jboolean ParseArguments(int *pargc, char ***pargv, 115 int *pmode, char **pwhat, 116 int *pret, const char *jrepath); 117 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv, 118 InvocationFunctions *ifn); 119 static jstring NewPlatformString(JNIEnv *env, char *s); 120 static jclass LoadMainClass(JNIEnv *env, int mode, char *name); 121 static jclass GetApplicationClass(JNIEnv *env); 122 123 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv); 124 static jboolean AddApplicationOptions(int cpathc, const char **cpathv); 125 static void SetApplicationClassPath(const char**); 126 127 static void PrintJavaVersion(JNIEnv *env); 128 static void PrintUsage(JNIEnv* env, jboolean doXUsage); 129 static void ShowSettings(JNIEnv* env, char *optString); 130 static void ShowResolvedModules(JNIEnv* env); 131 static void ListModules(JNIEnv* env); 132 static void DescribeModule(JNIEnv* env, char* optString); 133 static jboolean ValidateModules(JNIEnv* env); 134 135 static void SetPaths(int argc, char **argv); 136 137 static void DumpState(); 138 139 enum OptionKind { 140 LAUNCHER_OPTION = 0, 141 LAUNCHER_OPTION_WITH_ARGUMENT, 142 LAUNCHER_MAIN_OPTION, 143 VM_LONG_OPTION, 144 VM_LONG_OPTION_WITH_ARGUMENT, 145 VM_OPTION 146 }; 147 148 static int GetOpt(int *pargc, char ***pargv, char **poption, char **pvalue); 149 static jboolean IsOptionWithArgument(int argc, char **argv); 150 151 /* Maximum supported entries from jvm.cfg. */ 152 #define INIT_MAX_KNOWN_VMS 10 153 154 /* Values for vmdesc.flag */ 155 enum vmdesc_flag { 156 VM_UNKNOWN = -1, 157 VM_KNOWN, 158 VM_ALIASED_TO, 159 VM_WARN, 160 VM_ERROR, 161 VM_IF_SERVER_CLASS, 162 VM_IGNORE 163 }; 164 165 struct vmdesc { 166 char *name; 167 int flag; 168 char *alias; 169 char *server_class; 170 }; 171 static struct vmdesc *knownVMs = NULL; 172 static int knownVMsCount = 0; 173 static int knownVMsLimit = 0; 174 175 static void GrowKnownVMs(int minimum); 176 static int KnownVMIndex(const char* name); 177 static void FreeKnownVMs(); 178 static jboolean IsWildCardEnabled(); 179 180 181 #define SOURCE_LAUNCHER_MAIN_ENTRY "jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher" 182 183 /* 184 * This reports error. VM will not be created and no usage is printed. 185 */ 186 #define REPORT_ERROR(AC_ok, AC_failure_message, AC_questionable_arg) \ 187 do { \ 188 if (!AC_ok) { \ 189 JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \ 190 printUsage = JNI_FALSE; \ 191 *pret = 1; \ 192 return JNI_FALSE; \ 193 } \ 194 } while (JNI_FALSE) 195 196 #define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \ 197 do { \ 198 if (AC_arg_count < 1) { \ 199 JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \ 200 printUsage = JNI_TRUE; \ 201 *pret = 1; \ 202 return JNI_TRUE; \ 203 } \ 204 } while (JNI_FALSE) 205 206 /* 207 * Running Java code in primordial thread caused many problems. We will 208 * create a new thread to invoke JVM. See 6316197 for more information. 209 */ 210 static jlong threadStackSize = 0; /* stack size of the new thread */ 211 static jlong maxHeapSize = 0; /* max heap size */ 212 static jlong initialHeapSize = 0; /* initial heap size */ 213 214 /* 215 * A minimum initial-thread stack size suitable for most platforms. 216 * This is the minimum amount of stack needed to load the JVM such 217 * that it can reject a too small -Xss value. If this is too small 218 * JVM initialization would cause a StackOverflowError. 219 */ 220 #ifndef STACK_SIZE_MINIMUM 221 #define STACK_SIZE_MINIMUM (64 * KB) 222 #endif 223 224 /* 225 * Entry point. 226 */ 227 JNIEXPORT int JNICALL 228 JLI_Launch(int argc, char ** argv, /* main argc, argv */ 229 int jargc, const char** jargv, /* java args */ 230 int appclassc, const char** appclassv, /* app classpath */ 231 const char* fullversion, /* full version defined */ 232 const char* dotversion, /* UNUSED dot version defined */ 233 const char* pname, /* program name */ 234 const char* lname, /* launcher name */ 235 jboolean javaargs, /* JAVA_ARGS */ 236 jboolean cpwildcard, /* classpath wildcard*/ 237 jboolean javaw, /* windows-only javaw */ 238 jint ergo /* unused */ 239 ) 240 { 241 int mode = LM_UNKNOWN; 242 char *what = NULL; 243 char *main_class = NULL; 244 int ret; 245 InvocationFunctions ifn; 246 jlong start = 0, end = 0; 247 char jvmpath[MAXPATHLEN]; 248 char jrepath[MAXPATHLEN]; 249 char jvmcfg[MAXPATHLEN]; 250 251 _fVersion = fullversion; 252 _launcher_name = lname; 253 _program_name = pname; 254 _is_java_args = javaargs; 255 _wc_enabled = cpwildcard; 256 257 InitLauncher(javaw); 258 DumpState(); 259 if (JLI_IsTraceLauncher()) { 260 int i; 261 printf("Java args:\n"); 262 for (i = 0; i < jargc ; i++) { 263 printf("jargv[%d] = %s\n", i, jargv[i]); 264 } 265 printf("Command line args:\n"); 266 for (i = 0; i < argc ; i++) { 267 printf("argv[%d] = %s\n", i, argv[i]); 268 } 269 AddOption("-Dsun.java.launcher.diag=true", NULL); 270 } 271 272 /* 273 * SelectVersion() has several responsibilities: 274 * 275 * 1) Disallow specification of another JRE. With 1.9, another 276 * version of the JRE cannot be invoked. 277 * 2) Allow for a JRE version to invoke JDK 1.9 or later. Since 278 * all mJRE directives have been stripped from the request but 279 * the pre 1.9 JRE [ 1.6 thru 1.8 ], it is as if 1.9+ has been 280 * invoked from the command line. 281 */ 282 SelectVersion(argc, argv, &main_class); 283 284 CreateExecutionEnvironment(&argc, &argv, 285 jrepath, sizeof(jrepath), 286 jvmpath, sizeof(jvmpath), 287 jvmcfg, sizeof(jvmcfg)); 288 289 ifn.CreateJavaVM = 0; 290 ifn.GetDefaultJavaVMInitArgs = 0; 291 292 if (JLI_IsTraceLauncher()) { 293 start = CurrentTimeMicros(); 294 } 295 296 if (!LoadJavaVM(jvmpath, &ifn)) { 297 return(6); 298 } 299 300 if (JLI_IsTraceLauncher()) { 301 end = CurrentTimeMicros(); 302 } 303 304 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n", (long)(end-start)); 305 306 ++argv; 307 --argc; 308 309 if (IsJavaArgs()) { 310 /* Preprocess wrapper arguments */ 311 TranslateApplicationArgs(jargc, jargv, &argc, &argv); 312 if (!AddApplicationOptions(appclassc, appclassv)) { 313 return(1); 314 } 315 } else { 316 /* Set default CLASSPATH */ 317 char* cpath = getenv("CLASSPATH"); 318 if (cpath != NULL) { 319 SetClassPath(cpath); 320 } 321 } 322 323 /* Parse command line options; if the return value of 324 * ParseArguments is false, the program should exit. 325 */ 326 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath)) { 327 return(ret); 328 } 329 330 /* Override class path if -jar flag was specified */ 331 if (mode == LM_JAR) { 332 SetClassPath(what); /* Override class path */ 333 } 334 335 /* set the -Dsun.java.command pseudo property */ 336 SetJavaCommandLineProp(what, argc, argv); 337 338 /* Set the -Dsun.java.launcher pseudo property */ 339 SetJavaLauncherProp(); 340 341 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret); 342 } 343 /* 344 * Always detach the main thread so that it appears to have ended when 345 * the application's main method exits. This will invoke the 346 * uncaught exception handler machinery if main threw an 347 * exception. An uncaught exception handler cannot change the 348 * launcher's return code except by calling System.exit. 349 * 350 * Wait for all non-daemon threads to end, then destroy the VM. 351 * This will actually create a trivial new Java waiter thread 352 * named "DestroyJavaVM", but this will be seen as a different 353 * thread from the one that executed main, even though they are 354 * the same C thread. This allows mainThread.join() and 355 * mainThread.isAlive() to work as expected. 356 */ 357 #define LEAVE() \ 358 do { \ 359 if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \ 360 JLI_ReportErrorMessage(JVM_ERROR2); \ 361 ret = 1; \ 362 } \ 363 if (JNI_TRUE) { \ 364 (*vm)->DestroyJavaVM(vm); \ 365 return ret; \ 366 } \ 367 } while (JNI_FALSE) 368 369 #define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \ 370 do { \ 371 if ((*env)->ExceptionOccurred(env)) { \ 372 JLI_ReportExceptionDescription(env); \ 373 LEAVE(); \ 374 } \ 375 if ((CENL_exception) == NULL) { \ 376 JLI_ReportErrorMessage(JNI_ERROR); \ 377 LEAVE(); \ 378 } \ 379 } while (JNI_FALSE) 380 381 #define CHECK_EXCEPTION_LEAVE(CEL_return_value) \ 382 do { \ 383 if ((*env)->ExceptionOccurred(env)) { \ 384 JLI_ReportExceptionDescription(env); \ 385 ret = (CEL_return_value); \ 386 LEAVE(); \ 387 } \ 388 } while (JNI_FALSE) 389 390 /* 391 * Invokes static main(String[]) method if found. 392 * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe 393 * a pending exception if the method threw. 394 */ 395 int 396 invokeStaticMainWithArgs(JNIEnv *env, jclass mainClass, jobjectArray mainArgs) { 397 jmethodID mainID = (*env)->GetStaticMethodID(env, mainClass, "main", 398 "([Ljava/lang/String;)V"); 399 if (mainID == NULL) { 400 // static main(String[]) not found 401 return 0; 402 } 403 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs); 404 return 1; // method was invoked 405 } 406 407 /* 408 * Invokes instance main(String[]) method if found. 409 * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe 410 * a pending exception if the method threw. 411 */ 412 int 413 invokeInstanceMainWithArgs(JNIEnv *env, jclass mainClass, jobjectArray mainArgs) { 414 jmethodID constructor = (*env)->GetMethodID(env, mainClass, "<init>", "()V"); 415 if (constructor == NULL) { 416 // main class' no-arg constructor not found 417 return 0; 418 } 419 jobject mainObject = (*env)->NewObject(env, mainClass, constructor); 420 if (mainObject == NULL) { 421 // main class instance couldn't be constructed 422 return 0; 423 } 424 jmethodID mainID = 425 (*env)->GetMethodID(env, mainClass, "main", "([Ljava/lang/String;)V"); 426 if (mainID == NULL) { 427 // instance method main(String[]) method not found 428 return 0; 429 } 430 (*env)->CallVoidMethod(env, mainObject, mainID, mainArgs); 431 return 1; // method was invoked 432 } 433 434 /* 435 * Invokes no-arg static main() method if found. 436 * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe 437 * a pending exception if the method threw. 438 */ 439 int 440 invokeStaticMainWithoutArgs(JNIEnv *env, jclass mainClass) { 441 jmethodID mainID = (*env)->GetStaticMethodID(env, mainClass, "main", 442 "()V"); 443 if (mainID == NULL) { 444 // static main() method couldn't be located 445 return 0; 446 } 447 (*env)->CallStaticVoidMethod(env, mainClass, mainID); 448 return 1; // method was invoked 449 } 450 451 /* 452 * Invokes no-arg instance main() method if found. 453 * Returns 0 with a pending exception if not found. Returns 1 if invoked, maybe 454 * a pending exception if the method threw. 455 */ 456 int 457 invokeInstanceMainWithoutArgs(JNIEnv *env, jclass mainClass) { 458 jmethodID constructor = (*env)->GetMethodID(env, mainClass, "<init>", "()V"); 459 if (constructor == NULL) { 460 // main class' no-arg constructor not found 461 return 0; 462 } 463 jobject mainObject = (*env)->NewObject(env, mainClass, constructor); 464 if (mainObject == NULL) { 465 // couldn't create instance of main class 466 return 0; 467 } 468 jmethodID mainID = (*env)->GetMethodID(env, mainClass, "main", 469 "()V"); 470 if (mainID == NULL) { 471 // instance method main() not found 472 return 0; 473 } 474 (*env)->CallVoidMethod(env, mainObject, mainID); 475 return 1; // method was invoked 476 } 477 478 int 479 JavaMain(void* _args) 480 { 481 JavaMainArgs *args = (JavaMainArgs *)_args; 482 int argc = args->argc; 483 char **argv = args->argv; 484 int mode = args->mode; 485 char *what = args->what; 486 InvocationFunctions ifn = args->ifn; 487 488 JavaVM *vm = 0; 489 JNIEnv *env = 0; 490 jclass mainClass = NULL; 491 jclass appClass = NULL; // actual application class being launched 492 jobjectArray mainArgs; 493 int ret = 0; 494 jlong start = 0, end = 0; 495 jclass helperClass; 496 jfieldID isStaticMainField; 497 jboolean isStaticMain; 498 jfieldID noArgMainField; 499 jboolean noArgMain; 500 501 RegisterThread(); 502 503 /* Initialize the virtual machine */ 504 start = CurrentTimeMicros(); 505 if (!InitializeJVM(&vm, &env, &ifn)) { 506 JLI_ReportErrorMessage(JVM_ERROR1); 507 exit(1); 508 } 509 510 if (showSettings != NULL) { 511 ShowSettings(env, showSettings); 512 CHECK_EXCEPTION_LEAVE(1); 513 } 514 515 // show resolved modules and continue 516 if (showResolvedModules) { 517 ShowResolvedModules(env); 518 CHECK_EXCEPTION_LEAVE(1); 519 } 520 521 // list observable modules, then exit 522 if (listModules) { 523 ListModules(env); 524 CHECK_EXCEPTION_LEAVE(1); 525 LEAVE(); 526 } 527 528 // describe a module, then exit 529 if (describeModule != NULL) { 530 DescribeModule(env, describeModule); 531 CHECK_EXCEPTION_LEAVE(1); 532 LEAVE(); 533 } 534 535 if (printVersion || showVersion) { 536 PrintJavaVersion(env); 537 CHECK_EXCEPTION_LEAVE(0); 538 if (printVersion) { 539 LEAVE(); 540 } 541 } 542 543 // modules have been validated at startup so exit 544 if (validateModules) { 545 LEAVE(); 546 } 547 548 /* 549 * -Xshare:dump does not have a main class so the VM can safely exit now 550 */ 551 if (dumpSharedSpaces) { 552 CHECK_EXCEPTION_LEAVE(1); 553 LEAVE(); 554 } 555 556 /* If the user specified neither a class name nor a JAR file */ 557 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) { 558 PrintUsage(env, printXUsage); 559 CHECK_EXCEPTION_LEAVE(1); 560 LEAVE(); 561 } 562 563 FreeKnownVMs(); /* after last possible PrintUsage */ 564 565 if (JLI_IsTraceLauncher()) { 566 end = CurrentTimeMicros(); 567 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n", (long)(end-start)); 568 } 569 570 /* At this stage, argc/argv have the application's arguments */ 571 if (JLI_IsTraceLauncher()){ 572 int i; 573 printf("%s is '%s'\n", launchModeNames[mode], what); 574 printf("App's argc is %d\n", argc); 575 for (i=0; i < argc; i++) { 576 printf(" argv[%2d] = '%s'\n", i, argv[i]); 577 } 578 } 579 580 ret = 1; 581 582 /* 583 * See bugid 5030265. The Main-Class name has already been parsed 584 * from the manifest, but not parsed properly for UTF-8 support. 585 * Hence the code here ignores the value previously extracted and 586 * uses the pre-existing code to reextract the value. This is 587 * possibly an end of release cycle expedient. However, it has 588 * also been discovered that passing some character sets through 589 * the environment has "strange" behavior on some variants of 590 * Windows. Hence, maybe the manifest parsing code local to the 591 * launcher should never be enhanced. 592 * 593 * Hence, future work should either: 594 * 1) Correct the local parsing code and verify that the 595 * Main-Class attribute gets properly passed through 596 * all environments, 597 * 2) Remove the vestages of maintaining main_class through 598 * the environment (and remove these comments). 599 * 600 * This method also correctly handles launching existing JavaFX 601 * applications that may or may not have a Main-Class manifest entry. 602 */ 603 mainClass = LoadMainClass(env, mode, what); 604 CHECK_EXCEPTION_NULL_LEAVE(mainClass); 605 /* 606 * In some cases when launching an application that needs a helper, e.g., a 607 * JavaFX application with no main method, the mainClass will not be the 608 * applications own main class but rather a helper class. To keep things 609 * consistent in the UI we need to track and report the application main class. 610 */ 611 appClass = GetApplicationClass(env); 612 CHECK_EXCEPTION_NULL_LEAVE(appClass); 613 614 /* Build platform specific argument array */ 615 mainArgs = CreateApplicationArgs(env, argv, argc); 616 CHECK_EXCEPTION_NULL_LEAVE(mainArgs); 617 618 if (dryRun) { 619 ret = 0; 620 LEAVE(); 621 } 622 623 /* 624 * PostJVMInit uses the class name as the application name for GUI purposes, 625 * for example, on OSX this sets the application name in the menu bar for 626 * both SWT and JavaFX. So we'll pass the actual application class here 627 * instead of mainClass as that may be a launcher or helper class instead 628 * of the application class. 629 */ 630 PostJVMInit(env, appClass, vm); 631 CHECK_EXCEPTION_LEAVE(1); 632 633 /* 634 * The main method is invoked here so that extraneous java stacks are not in 635 * the application stack trace. 636 */ 637 638 helperClass = GetLauncherHelperClass(env); 639 isStaticMainField = (*env)->GetStaticFieldID(env, helperClass, "isStaticMain", "Z"); 640 CHECK_EXCEPTION_NULL_LEAVE(isStaticMainField); 641 isStaticMain = (*env)->GetStaticBooleanField(env, helperClass, isStaticMainField); 642 643 noArgMainField = (*env)->GetStaticFieldID(env, helperClass, "noArgMain", "Z"); 644 CHECK_EXCEPTION_NULL_LEAVE(noArgMainField); 645 noArgMain = (*env)->GetStaticBooleanField(env, helperClass, noArgMainField); 646 647 if (isStaticMain) { 648 if (noArgMain) { 649 ret = invokeStaticMainWithoutArgs(env, mainClass); 650 } else { 651 ret = invokeStaticMainWithArgs(env, mainClass, mainArgs); 652 } 653 } else { 654 if (noArgMain) { 655 ret = invokeInstanceMainWithoutArgs(env, mainClass); 656 } else { 657 ret = invokeInstanceMainWithArgs(env, mainClass, mainArgs); 658 } 659 } 660 if (!ret) { 661 // An appropriate main method couldn't be located, check and report 662 // any exception and LEAVE() 663 CHECK_EXCEPTION_LEAVE(1); 664 } 665 666 /* 667 * The launcher's exit code (in the absence of calls to 668 * System.exit) will be non-zero if main threw an exception. 669 */ 670 if (ret && (*env)->ExceptionOccurred(env) == NULL) { 671 // main method was invoked and no exception was thrown from it, 672 // return success. 673 ret = 0; 674 } else { 675 // Either the main method couldn't be located or an exception occurred 676 // in the invoked main method, return failure. 677 ret = 1; 678 } 679 LEAVE(); 680 } 681 682 /* 683 * Test if the given name is one of the class path options. 684 */ 685 static jboolean 686 IsClassPathOption(const char* name) { 687 return JLI_StrCmp(name, "-classpath") == 0 || 688 JLI_StrCmp(name, "-cp") == 0 || 689 JLI_StrCmp(name, "--class-path") == 0; 690 } 691 692 /* 693 * Test if the given name is a launcher option taking the main entry point. 694 */ 695 static jboolean 696 IsLauncherMainOption(const char* name) { 697 return JLI_StrCmp(name, "--module") == 0 || 698 JLI_StrCmp(name, "-m") == 0; 699 } 700 701 /* 702 * Test if the given name is a white-space launcher option. 703 */ 704 static jboolean 705 IsLauncherOption(const char* name) { 706 return IsClassPathOption(name) || 707 IsLauncherMainOption(name) || 708 JLI_StrCmp(name, "--describe-module") == 0 || 709 JLI_StrCmp(name, "-d") == 0 || 710 JLI_StrCmp(name, "--source") == 0; 711 } 712 713 /* 714 * Test if the given name is a module-system white-space option that 715 * will be passed to the VM with its corresponding long-form option 716 * name and "=" delimiter. 717 */ 718 static jboolean 719 IsModuleOption(const char* name) { 720 return JLI_StrCmp(name, "--module-path") == 0 || 721 JLI_StrCmp(name, "-p") == 0 || 722 JLI_StrCmp(name, "--upgrade-module-path") == 0 || 723 JLI_StrCmp(name, "--add-modules") == 0 || 724 JLI_StrCmp(name, "--enable-native-access") == 0 || 725 JLI_StrCmp(name, "--limit-modules") == 0 || 726 JLI_StrCmp(name, "--add-exports") == 0 || 727 JLI_StrCmp(name, "--add-opens") == 0 || 728 JLI_StrCmp(name, "--add-reads") == 0 || 729 JLI_StrCmp(name, "--patch-module") == 0; 730 } 731 732 static jboolean 733 IsLongFormModuleOption(const char* name) { 734 return JLI_StrCCmp(name, "--module-path=") == 0 || 735 JLI_StrCCmp(name, "--upgrade-module-path=") == 0 || 736 JLI_StrCCmp(name, "--add-modules=") == 0 || 737 JLI_StrCCmp(name, "--enable-native-access=") == 0 || 738 JLI_StrCCmp(name, "--limit-modules=") == 0 || 739 JLI_StrCCmp(name, "--add-exports=") == 0 || 740 JLI_StrCCmp(name, "--add-reads=") == 0 || 741 JLI_StrCCmp(name, "--patch-module=") == 0; 742 } 743 744 /* 745 * Test if the given name has a white space option. 746 */ 747 jboolean 748 IsWhiteSpaceOption(const char* name) { 749 return IsModuleOption(name) || 750 IsLauncherOption(name); 751 } 752 753 /* 754 * Check if it is OK to set the mode. 755 * If the mode was previously set, and should not be changed, 756 * a fatal error is reported. 757 */ 758 static int 759 checkMode(int mode, int newMode, const char *arg) { 760 if (mode == LM_SOURCE) { 761 JLI_ReportErrorMessage(ARG_ERROR14, arg); 762 exit(1); 763 } 764 return newMode; 765 } 766 767 /* 768 * Test if an arg identifies a source file. 769 */ 770 static jboolean IsSourceFile(const char *arg) { 771 struct stat st; 772 return (JLI_HasSuffix(arg, ".java") && stat(arg, &st) == 0); 773 } 774 775 /* 776 * Checks the command line options to find which JVM type was 777 * specified. If no command line option was given for the JVM type, 778 * the default type is used. The environment variable 779 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also 780 * checked as ways of specifying which JVM type to invoke. 781 */ 782 char * 783 CheckJvmType(int *pargc, char ***argv, jboolean speculative) { 784 int i, argi; 785 int argc; 786 char **newArgv; 787 int newArgvIdx = 0; 788 int isVMType; 789 int jvmidx = -1; 790 char *jvmtype = getenv("JDK_ALTERNATE_VM"); 791 792 argc = *pargc; 793 794 /* To make things simpler we always copy the argv array */ 795 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *)); 796 797 /* The program name is always present */ 798 newArgv[newArgvIdx++] = (*argv)[0]; 799 800 for (argi = 1; argi < argc; argi++) { 801 char *arg = (*argv)[argi]; 802 isVMType = 0; 803 804 if (IsJavaArgs()) { 805 if (arg[0] != '-') { 806 newArgv[newArgvIdx++] = arg; 807 continue; 808 } 809 } else { 810 if (IsWhiteSpaceOption(arg)) { 811 newArgv[newArgvIdx++] = arg; 812 argi++; 813 if (argi < argc) { 814 newArgv[newArgvIdx++] = (*argv)[argi]; 815 } 816 continue; 817 } 818 if (arg[0] != '-') break; 819 } 820 821 /* Did the user pass an explicit VM type? */ 822 i = KnownVMIndex(arg); 823 if (i >= 0) { 824 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */ 825 isVMType = 1; 826 *pargc = *pargc - 1; 827 } 828 829 /* Did the user specify an "alternate" VM? */ 830 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) { 831 isVMType = 1; 832 jvmtype = arg+((arg[1]=='X')? 10 : 12); 833 jvmidx = -1; 834 } 835 836 if (!isVMType) { 837 newArgv[newArgvIdx++] = arg; 838 } 839 } 840 841 /* 842 * Finish copying the arguments if we aborted the above loop. 843 * NOTE that if we aborted via "break" then we did NOT copy the 844 * last argument above, and in addition argi will be less than 845 * argc. 846 */ 847 while (argi < argc) { 848 newArgv[newArgvIdx++] = (*argv)[argi]; 849 argi++; 850 } 851 852 /* argv is null-terminated */ 853 newArgv[newArgvIdx] = 0; 854 855 /* Copy back argv */ 856 *argv = newArgv; 857 *pargc = newArgvIdx; 858 859 /* use the default VM type if not specified (no alias processing) */ 860 if (jvmtype == NULL) { 861 char* result = knownVMs[0].name+1; 862 JLI_TraceLauncher("Default VM: %s\n", result); 863 return result; 864 } 865 866 /* if using an alternate VM, no alias processing */ 867 if (jvmidx < 0) 868 return jvmtype; 869 870 /* Resolve aliases first */ 871 { 872 int loopCount = 0; 873 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) { 874 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias); 875 876 if (loopCount > knownVMsCount) { 877 if (!speculative) { 878 JLI_ReportErrorMessage(CFG_ERROR1); 879 exit(1); 880 } else { 881 return "ERROR"; 882 /* break; */ 883 } 884 } 885 886 if (nextIdx < 0) { 887 if (!speculative) { 888 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias); 889 exit(1); 890 } else { 891 return "ERROR"; 892 } 893 } 894 jvmidx = nextIdx; 895 jvmtype = knownVMs[jvmidx].name+1; 896 loopCount++; 897 } 898 } 899 900 switch (knownVMs[jvmidx].flag) { 901 case VM_WARN: 902 if (!speculative) { 903 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1); 904 } 905 /* fall through */ 906 case VM_IGNORE: 907 jvmtype = knownVMs[jvmidx=0].name + 1; 908 /* fall through */ 909 case VM_KNOWN: 910 break; 911 case VM_ERROR: 912 if (!speculative) { 913 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype); 914 exit(1); 915 } else { 916 return "ERROR"; 917 } 918 } 919 920 return jvmtype; 921 } 922 923 /* copied from HotSpot function "atomll()" */ 924 static int 925 parse_size(const char *s, jlong *result) { 926 jlong n = 0; 927 int args_read = sscanf(s, JLONG_FORMAT_SPECIFIER, &n); 928 if (args_read != 1) { 929 return 0; 930 } 931 while (*s != '\0' && *s >= '0' && *s <= '9') { 932 s++; 933 } 934 // 4705540: illegal if more characters are found after the first non-digit 935 if (JLI_StrLen(s) > 1) { 936 return 0; 937 } 938 switch (*s) { 939 case 'T': case 't': 940 *result = n * GB * KB; 941 return 1; 942 case 'G': case 'g': 943 *result = n * GB; 944 return 1; 945 case 'M': case 'm': 946 *result = n * MB; 947 return 1; 948 case 'K': case 'k': 949 *result = n * KB; 950 return 1; 951 case '\0': 952 *result = n; 953 return 1; 954 default: 955 /* Create JVM with default stack and let VM handle malformed -Xss string*/ 956 return 0; 957 } 958 } 959 960 /* 961 * Adds a new VM option with the given name and value. 962 */ 963 void 964 AddOption(char *str, void *info) 965 { 966 /* 967 * Expand options array if needed to accommodate at least one more 968 * VM option. 969 */ 970 if (numOptions >= maxOptions) { 971 if (options == 0) { 972 maxOptions = 4; 973 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption)); 974 } else { 975 JavaVMOption *tmp; 976 maxOptions *= 2; 977 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption)); 978 memcpy(tmp, options, numOptions * sizeof(JavaVMOption)); 979 JLI_MemFree(options); 980 options = tmp; 981 } 982 } 983 options[numOptions].optionString = str; 984 options[numOptions++].extraInfo = info; 985 986 /* 987 * -Xss is used both by the JVM and here to establish the stack size of the thread 988 * created to launch the JVM. In the latter case we need to ensure we don't go 989 * below the minimum stack size allowed. If -Xss is zero that tells the JVM to use 990 * 'default' sizes (either from JVM or system configuration, e.g. 'ulimit -s' on linux), 991 * and is not itself a small stack size that will be rejected. So we ignore -Xss0 here. 992 */ 993 if (JLI_StrCCmp(str, "-Xss") == 0) { 994 jlong tmp; 995 if (parse_size(str + 4, &tmp)) { 996 threadStackSize = tmp; 997 if (threadStackSize > 0 && threadStackSize < (jlong)STACK_SIZE_MINIMUM) { 998 threadStackSize = STACK_SIZE_MINIMUM; 999 } 1000 } 1001 } 1002 1003 if (JLI_StrCCmp(str, "-Xmx") == 0) { 1004 jlong tmp; 1005 if (parse_size(str + 4, &tmp)) { 1006 maxHeapSize = tmp; 1007 } 1008 } 1009 1010 if (JLI_StrCCmp(str, "-Xms") == 0) { 1011 jlong tmp; 1012 if (parse_size(str + 4, &tmp)) { 1013 initialHeapSize = tmp; 1014 } 1015 } 1016 } 1017 1018 static void 1019 SetClassPath(const char *s) 1020 { 1021 char *def; 1022 const char *orig = s; 1023 static const char format[] = "-Djava.class.path=%s"; 1024 /* 1025 * usually we should not get a null pointer, but there are cases where 1026 * we might just get one, in which case we simply ignore it, and let the 1027 * caller deal with it 1028 */ 1029 if (s == NULL) 1030 return; 1031 s = JLI_WildcardExpandClasspath(s); 1032 if (sizeof(format) - 2 + JLI_StrLen(s) < JLI_StrLen(s)) 1033 // s is became corrupted after expanding wildcards 1034 return; 1035 size_t defSize = sizeof(format) 1036 - 2 /* strlen("%s") */ 1037 + JLI_StrLen(s); 1038 def = JLI_MemAlloc(defSize); 1039 snprintf(def, defSize, format, s); 1040 AddOption(def, NULL); 1041 if (s != orig) 1042 JLI_MemFree((char *) s); 1043 _have_classpath = JNI_TRUE; 1044 } 1045 1046 static void 1047 AddLongFormOption(const char *option, const char *arg) 1048 { 1049 static const char format[] = "%s=%s"; 1050 char *def; 1051 size_t def_len; 1052 1053 def_len = JLI_StrLen(option) + 1 + JLI_StrLen(arg) + 1; 1054 def = JLI_MemAlloc(def_len); 1055 JLI_Snprintf(def, def_len, format, option, arg); 1056 AddOption(def, NULL); 1057 } 1058 1059 static void 1060 SetMainModule(const char *s) 1061 { 1062 static const char format[] = "-Djdk.module.main=%s"; 1063 char* slash = JLI_StrChr(s, '/'); 1064 size_t s_len, def_len; 1065 char *def; 1066 1067 /* value may be <module> or <module>/<mainclass> */ 1068 if (slash == NULL) { 1069 s_len = JLI_StrLen(s); 1070 } else { 1071 s_len = (size_t) (slash - s); 1072 } 1073 def_len = sizeof(format) 1074 - 2 /* strlen("%s") */ 1075 + s_len; 1076 def = JLI_MemAlloc(def_len); 1077 JLI_Snprintf(def, def_len, format, s); 1078 AddOption(def, NULL); 1079 } 1080 1081 /* 1082 * The SelectVersion() routine ensures that an appropriate version of 1083 * the JRE is running. The specification for the appropriate version 1084 * is obtained from either the manifest of a jar file (preferred) or 1085 * from command line options. 1086 * The routine also parses splash screen command line options and 1087 * passes on their values in private environment variables. 1088 */ 1089 static void 1090 SelectVersion(int argc, char **argv, char **main_class) 1091 { 1092 char *arg; 1093 char *operand; 1094 int jarflag = 0; 1095 int headlessflag = 0; 1096 manifest_info info; 1097 char *splash_file_name = NULL; 1098 char *splash_jar_name = NULL; 1099 char *env_in; 1100 int res; 1101 jboolean has_arg; 1102 1103 /* 1104 * If the version has already been selected, set *main_class 1105 * with the value passed through the environment (if any) and 1106 * simply return. 1107 */ 1108 1109 /* 1110 * This environmental variable can be set by mJRE capable JREs 1111 * [ 1.5 thru 1.8 ]. All other aspects of mJRE processing have been 1112 * stripped by those JREs. This environmental variable allows 1.9+ 1113 * JREs to be started by these mJRE capable JREs. 1114 * Note that mJRE directives in the jar manifest file would have been 1115 * ignored for a JRE started by another JRE... 1116 * .. skipped for JRE 1.5 and beyond. 1117 * .. not even checked for pre 1.5. 1118 */ 1119 if ((env_in = getenv(ENV_ENTRY)) != NULL) { 1120 if (*env_in != '\0') 1121 *main_class = JLI_StringDup(env_in); 1122 return; 1123 } 1124 1125 /* 1126 * Scan through the arguments for options relevant to multiple JRE 1127 * support. Multiple JRE support existed in JRE versions 1.5 thru 1.8. 1128 * 1129 * This capability is no longer available with JRE versions 1.9 and later. 1130 * These command line options are reported as errors. 1131 */ 1132 1133 argc--; 1134 argv++; 1135 while (argc > 0 && *(arg = *argv) == '-') { 1136 has_arg = IsOptionWithArgument(argc, argv); 1137 if (JLI_StrCCmp(arg, "-version:") == 0) { 1138 JLI_ReportErrorMessage(SPC_ERROR1); 1139 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) { 1140 JLI_ReportErrorMessage(SPC_ERROR2); 1141 } else if (JLI_StrCmp(arg, "-jre-no-restrict-search") == 0) { 1142 JLI_ReportErrorMessage(SPC_ERROR2); 1143 } else { 1144 if (JLI_StrCmp(arg, "-jar") == 0) 1145 jarflag = 1; 1146 if (IsWhiteSpaceOption(arg)) { 1147 if (has_arg) { 1148 argc--; 1149 argv++; 1150 arg = *argv; 1151 } 1152 } 1153 1154 /* 1155 * Checking for headless toolkit option in the some way as AWT does: 1156 * "true" means true and any other value means false 1157 */ 1158 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) { 1159 headlessflag = 1; 1160 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) { 1161 headlessflag = 0; 1162 } else if (JLI_StrCCmp(arg, "-splash:") == 0) { 1163 splash_file_name = arg+8; 1164 } 1165 } 1166 argc--; 1167 argv++; 1168 } 1169 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */ 1170 operand = NULL; 1171 } else { 1172 argc--; 1173 operand = *argv++; 1174 } 1175 1176 /* 1177 * If there is a jar file, read the manifest. If the jarfile can't be 1178 * read, the manifest can't be read from the jar file, or the manifest 1179 * is corrupt, issue the appropriate error messages and exit. 1180 * 1181 * Even if there isn't a jar file, construct a manifest_info structure 1182 * containing the command line information. It's a convenient way to carry 1183 * this data around. 1184 */ 1185 if (jarflag && operand) { 1186 if ((res = JLI_ParseManifest(operand, &info)) != 0) { 1187 if (res == -1) 1188 JLI_ReportErrorMessage(JAR_ERROR2, operand); 1189 else 1190 JLI_ReportErrorMessage(JAR_ERROR3, operand); 1191 exit(1); 1192 } 1193 1194 /* 1195 * Command line splash screen option should have precedence 1196 * over the manifest, so the manifest data is used only if 1197 * splash_file_name has not been initialized above during command 1198 * line parsing 1199 */ 1200 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) { 1201 splash_file_name = info.splashscreen_image_file_name; 1202 splash_jar_name = operand; 1203 } 1204 } else { 1205 info.manifest_version = NULL; 1206 info.main_class = NULL; 1207 info.jre_version = NULL; 1208 info.jre_restrict_search = 0; 1209 } 1210 1211 /* 1212 * Passing on splash screen info in environment variables 1213 */ 1214 if (splash_file_name && !headlessflag) { 1215 splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1); 1216 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "="); 1217 JLI_StrCat(splash_file_entry, splash_file_name); 1218 putenv(splash_file_entry); 1219 } 1220 if (splash_jar_name && !headlessflag) { 1221 splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1); 1222 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "="); 1223 JLI_StrCat(splash_jar_entry, splash_jar_name); 1224 putenv(splash_jar_entry); 1225 } 1226 1227 1228 /* 1229 * "Valid" returns (other than unrecoverable errors) follow. Set 1230 * main_class as a side-effect of this routine. 1231 */ 1232 if (info.main_class != NULL) 1233 *main_class = JLI_StringDup(info.main_class); 1234 1235 if (info.jre_version == NULL) { 1236 JLI_FreeManifest(); 1237 return; 1238 } 1239 1240 } 1241 1242 /* 1243 * Test if the current argv is an option, i.e. with a leading `-` 1244 * and followed with an argument without a leading `-`. 1245 */ 1246 static jboolean 1247 IsOptionWithArgument(int argc, char** argv) { 1248 char* option; 1249 char* arg; 1250 1251 if (argc <= 1) 1252 return JNI_FALSE; 1253 1254 option = *argv; 1255 arg = *(argv+1); 1256 return *option == '-' && *arg != '-'; 1257 } 1258 1259 /* 1260 * Gets the option, and its argument if the option has an argument. 1261 * It will update *pargc, **pargv to the next option. 1262 */ 1263 static int 1264 GetOpt(int *pargc, char ***pargv, char **poption, char **pvalue) { 1265 int argc = *pargc; 1266 char** argv = *pargv; 1267 char* arg = *argv; 1268 1269 char* option = arg; 1270 char* value = NULL; 1271 char* equals = NULL; 1272 int kind = LAUNCHER_OPTION; 1273 jboolean has_arg = JNI_FALSE; 1274 1275 // check if this option may be a white-space option with an argument 1276 has_arg = IsOptionWithArgument(argc, argv); 1277 1278 argv++; --argc; 1279 if (IsLauncherOption(arg)) { 1280 if (has_arg) { 1281 value = *argv; 1282 argv++; --argc; 1283 } 1284 kind = IsLauncherMainOption(arg) ? LAUNCHER_MAIN_OPTION 1285 : LAUNCHER_OPTION_WITH_ARGUMENT; 1286 } else if (IsModuleOption(arg)) { 1287 kind = VM_LONG_OPTION_WITH_ARGUMENT; 1288 if (has_arg) { 1289 value = *argv; 1290 argv++; --argc; 1291 } 1292 1293 /* 1294 * Support short form alias 1295 */ 1296 if (JLI_StrCmp(arg, "-p") == 0) { 1297 option = "--module-path"; 1298 } 1299 1300 } else if (JLI_StrCCmp(arg, "--") == 0 && (equals = JLI_StrChr(arg, '=')) != NULL) { 1301 value = equals+1; 1302 if (JLI_StrCCmp(arg, "--describe-module=") == 0 || 1303 JLI_StrCCmp(arg, "--module=") == 0 || 1304 JLI_StrCCmp(arg, "--class-path=") == 0|| 1305 JLI_StrCCmp(arg, "--source=") == 0) { 1306 kind = LAUNCHER_OPTION_WITH_ARGUMENT; 1307 } else { 1308 kind = VM_LONG_OPTION; 1309 } 1310 } 1311 1312 *pargc = argc; 1313 *pargv = argv; 1314 *poption = option; 1315 *pvalue = value; 1316 return kind; 1317 } 1318 1319 /* 1320 * Parses command line arguments. Returns JNI_FALSE if launcher 1321 * should exit without starting vm, returns JNI_TRUE if vm needs 1322 * to be started to process given options. *pret (the launcher 1323 * process return value) is set to 0 for a normal exit. 1324 */ 1325 static jboolean 1326 ParseArguments(int *pargc, char ***pargv, 1327 int *pmode, char **pwhat, 1328 int *pret, const char *jrepath) 1329 { 1330 int argc = *pargc; 1331 char **argv = *pargv; 1332 int mode = LM_UNKNOWN; 1333 char *arg = NULL; 1334 1335 *pret = 0; 1336 1337 while (argc > 0 && *(arg = *argv) == '-') { 1338 char *option = NULL; 1339 char *value = NULL; 1340 int kind = GetOpt(&argc, &argv, &option, &value); 1341 jboolean has_arg = value != NULL && JLI_StrLen(value) > 0; 1342 jboolean has_arg_any_len = value != NULL; 1343 1344 /* 1345 * Option to set main entry point 1346 */ 1347 if (JLI_StrCmp(arg, "-jar") == 0) { 1348 ARG_CHECK(argc, ARG_ERROR2, arg); 1349 mode = checkMode(mode, LM_JAR, arg); 1350 } else if (JLI_StrCmp(arg, "--module") == 0 || 1351 JLI_StrCCmp(arg, "--module=") == 0 || 1352 JLI_StrCmp(arg, "-m") == 0) { 1353 REPORT_ERROR (has_arg, ARG_ERROR5, arg); 1354 SetMainModule(value); 1355 mode = checkMode(mode, LM_MODULE, arg); 1356 if (has_arg) { 1357 *pwhat = value; 1358 break; 1359 } 1360 } else if (JLI_StrCmp(arg, "--source") == 0 || 1361 JLI_StrCCmp(arg, "--source=") == 0) { 1362 REPORT_ERROR (has_arg, ARG_ERROR13, arg); 1363 mode = LM_SOURCE; 1364 if (has_arg) { 1365 const char *prop = "-Djdk.internal.javac.source="; 1366 size_t size = JLI_StrLen(prop) + JLI_StrLen(value) + 1; 1367 char *propValue = (char *)JLI_MemAlloc(size); 1368 JLI_Snprintf(propValue, size, "%s%s", prop, value); 1369 AddOption(propValue, NULL); 1370 } 1371 } else if (JLI_StrCmp(arg, "--class-path") == 0 || 1372 JLI_StrCCmp(arg, "--class-path=") == 0 || 1373 JLI_StrCmp(arg, "-classpath") == 0 || 1374 JLI_StrCmp(arg, "-cp") == 0) { 1375 REPORT_ERROR (has_arg_any_len, ARG_ERROR1, arg); 1376 SetClassPath(value); 1377 if (mode != LM_SOURCE) { 1378 mode = LM_CLASS; 1379 } 1380 } else if (JLI_StrCmp(arg, "--list-modules") == 0) { 1381 listModules = JNI_TRUE; 1382 } else if (JLI_StrCmp(arg, "--show-resolved-modules") == 0) { 1383 showResolvedModules = JNI_TRUE; 1384 } else if (JLI_StrCmp(arg, "--validate-modules") == 0) { 1385 AddOption("-Djdk.module.validation=true", NULL); 1386 validateModules = JNI_TRUE; 1387 } else if (JLI_StrCmp(arg, "--describe-module") == 0 || 1388 JLI_StrCCmp(arg, "--describe-module=") == 0 || 1389 JLI_StrCmp(arg, "-d") == 0) { 1390 REPORT_ERROR (has_arg_any_len, ARG_ERROR12, arg); 1391 describeModule = value; 1392 /* 1393 * Parse white-space options 1394 */ 1395 } else if (has_arg) { 1396 if (kind == VM_LONG_OPTION) { 1397 AddOption(option, NULL); 1398 } else if (kind == VM_LONG_OPTION_WITH_ARGUMENT) { 1399 AddLongFormOption(option, value); 1400 } 1401 /* 1402 * Error missing argument 1403 */ 1404 } else if (!has_arg && (JLI_StrCmp(arg, "--module-path") == 0 || 1405 JLI_StrCmp(arg, "-p") == 0 || 1406 JLI_StrCmp(arg, "--upgrade-module-path") == 0)) { 1407 REPORT_ERROR (has_arg, ARG_ERROR4, arg); 1408 1409 } else if (!has_arg && (IsModuleOption(arg) || IsLongFormModuleOption(arg))) { 1410 REPORT_ERROR (has_arg, ARG_ERROR6, arg); 1411 /* 1412 * The following cases will cause the argument parsing to stop 1413 */ 1414 } else if (JLI_StrCmp(arg, "-help") == 0 || 1415 JLI_StrCmp(arg, "-h") == 0 || 1416 JLI_StrCmp(arg, "-?") == 0) { 1417 printUsage = JNI_TRUE; 1418 return JNI_TRUE; 1419 } else if (JLI_StrCmp(arg, "--help") == 0) { 1420 printUsage = JNI_TRUE; 1421 printTo = USE_STDOUT; 1422 return JNI_TRUE; 1423 } else if (JLI_StrCmp(arg, "-version") == 0) { 1424 printVersion = JNI_TRUE; 1425 return JNI_TRUE; 1426 } else if (JLI_StrCmp(arg, "--version") == 0) { 1427 printVersion = JNI_TRUE; 1428 printTo = USE_STDOUT; 1429 return JNI_TRUE; 1430 } else if (JLI_StrCmp(arg, "-showversion") == 0) { 1431 showVersion = JNI_TRUE; 1432 } else if (JLI_StrCmp(arg, "--show-version") == 0) { 1433 showVersion = JNI_TRUE; 1434 printTo = USE_STDOUT; 1435 } else if (JLI_StrCmp(arg, "--dry-run") == 0) { 1436 dryRun = JNI_TRUE; 1437 } else if (JLI_StrCmp(arg, "-X") == 0) { 1438 printXUsage = JNI_TRUE; 1439 return JNI_TRUE; 1440 } else if (JLI_StrCmp(arg, "--help-extra") == 0) { 1441 printXUsage = JNI_TRUE; 1442 printTo = USE_STDOUT; 1443 return JNI_TRUE; 1444 /* 1445 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT. 1446 * In the latter case, any SUBOPT value not recognized will default to "all" 1447 */ 1448 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 || 1449 JLI_StrCCmp(arg, "-XshowSettings:") == 0) { 1450 showSettings = arg; 1451 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) { 1452 AddOption("-Dsun.java.launcher.diag=true", NULL); 1453 } else if (JLI_StrCmp(arg, "--show-module-resolution") == 0) { 1454 AddOption("-Djdk.module.showModuleResolution=true", NULL); 1455 /* 1456 * The following case provide backward compatibility with old-style 1457 * command line options. 1458 */ 1459 } else if (JLI_StrCmp(arg, "-fullversion") == 0) { 1460 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion()); 1461 return JNI_FALSE; 1462 } else if (JLI_StrCmp(arg, "--full-version") == 0) { 1463 JLI_ShowMessage("%s %s", _launcher_name, GetFullVersion()); 1464 return JNI_FALSE; 1465 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) { 1466 AddOption("-verbose:gc", NULL); 1467 } else if (JLI_StrCmp(arg, "-t") == 0) { 1468 AddOption("-Xt", NULL); 1469 } else if (JLI_StrCmp(arg, "-tm") == 0) { 1470 AddOption("-Xtm", NULL); 1471 } else if (JLI_StrCmp(arg, "-debug") == 0) { 1472 JLI_ReportErrorMessage(ARG_DEPRECATED, "-debug"); 1473 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) { 1474 AddOption("-Xnoclassgc", NULL); 1475 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) { 1476 JLI_ReportErrorMessage(ARG_DEPRECATED, "-Xfuture"); 1477 AddOption("-Xverify:all", NULL); 1478 } else if (JLI_StrCmp(arg, "-verify") == 0) { 1479 AddOption("-Xverify:all", NULL); 1480 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) { 1481 AddOption("-Xverify:remote", NULL); 1482 } else if (JLI_StrCmp(arg, "-noverify") == 0) { 1483 /* 1484 * Note that no 'deprecated' message is needed here because the VM 1485 * issues 'deprecated' messages for -noverify and -Xverify:none. 1486 */ 1487 AddOption("-Xverify:none", NULL); 1488 } else if (JLI_StrCCmp(arg, "-ss") == 0 || 1489 JLI_StrCCmp(arg, "-oss") == 0 || 1490 JLI_StrCCmp(arg, "-ms") == 0 || 1491 JLI_StrCCmp(arg, "-mx") == 0) { 1492 size_t tmpSize = JLI_StrLen(arg) + 6; 1493 char *tmp = JLI_MemAlloc(tmpSize); 1494 snprintf(tmp, tmpSize, "-X%s", arg + 1); /* skip '-' */ 1495 AddOption(tmp, NULL); 1496 } else if (JLI_StrCmp(arg, "-checksource") == 0 || 1497 JLI_StrCmp(arg, "-cs") == 0 || 1498 JLI_StrCmp(arg, "-noasyncgc") == 0) { 1499 /* No longer supported */ 1500 JLI_ReportErrorMessage(ARG_WARN, arg); 1501 } else if (JLI_StrCCmp(arg, "-splash:") == 0) { 1502 ; /* Ignore machine independent options already handled */ 1503 } else if (JLI_StrCmp(arg, "--disable-@files") == 0) { 1504 ; /* Ignore --disable-@files option already handled */ 1505 } else if (ProcessPlatformOption(arg)) { 1506 ; /* Processing of platform dependent options */ 1507 } else { 1508 /* java.class.path set on the command line */ 1509 if (JLI_StrCCmp(arg, "-Djava.class.path=") == 0) { 1510 _have_classpath = JNI_TRUE; 1511 } 1512 AddOption(arg, NULL); 1513 } 1514 1515 /* 1516 * Check for CDS option 1517 */ 1518 if (JLI_StrCmp(arg, "-Xshare:dump") == 0) { 1519 dumpSharedSpaces = JNI_TRUE; 1520 } 1521 } 1522 1523 if (*pwhat == NULL && --argc >= 0) { 1524 *pwhat = *argv++; 1525 } 1526 1527 if (*pwhat == NULL) { 1528 /* LM_UNKNOWN okay for options that exit */ 1529 if (!listModules && !describeModule && !validateModules && !dumpSharedSpaces) { 1530 *pret = 1; 1531 } 1532 } else if (mode == LM_UNKNOWN) { 1533 /* default to LM_CLASS if -m, -jar and -cp options are 1534 * not specified */ 1535 if (!_have_classpath) { 1536 SetClassPath("."); 1537 } 1538 mode = IsSourceFile(arg) ? LM_SOURCE : LM_CLASS; 1539 } else if (mode == LM_CLASS && IsSourceFile(arg)) { 1540 /* override LM_CLASS mode if given a source file */ 1541 mode = LM_SOURCE; 1542 } 1543 1544 if (mode == LM_SOURCE) { 1545 AddOption("--add-modules=ALL-DEFAULT", NULL); 1546 *pwhat = SOURCE_LAUNCHER_MAIN_ENTRY; 1547 // adjust (argc, argv) so that the name of the source file 1548 // is included in the args passed to the source launcher 1549 // main entry class 1550 *pargc = argc + 1; 1551 *pargv = argv - 1; 1552 } else { 1553 if (argc >= 0) { 1554 *pargc = argc; 1555 *pargv = argv; 1556 } 1557 } 1558 1559 *pmode = mode; 1560 1561 return JNI_TRUE; 1562 } 1563 1564 /* 1565 * Initializes the Java Virtual Machine. Also frees options array when 1566 * finished. 1567 */ 1568 static jboolean 1569 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn) 1570 { 1571 JavaVMInitArgs args; 1572 jint r; 1573 1574 memset(&args, 0, sizeof(args)); 1575 args.version = JNI_VERSION_1_2; 1576 args.nOptions = numOptions; 1577 args.options = options; 1578 args.ignoreUnrecognized = JNI_FALSE; 1579 1580 if (JLI_IsTraceLauncher()) { 1581 int i = 0; 1582 printf("JavaVM args:\n "); 1583 printf("version 0x%08lx, ", (long)args.version); 1584 printf("ignoreUnrecognized is %s, ", 1585 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE"); 1586 printf("nOptions is %ld\n", (long)args.nOptions); 1587 for (i = 0; i < numOptions; i++) 1588 printf(" option[%2d] = '%s'\n", 1589 i, args.options[i].optionString); 1590 } 1591 1592 r = ifn->CreateJavaVM(pvm, (void **)penv, &args); 1593 JLI_MemFree(options); 1594 return r == JNI_OK; 1595 } 1596 1597 static jclass helperClass = NULL; 1598 1599 jclass 1600 GetLauncherHelperClass(JNIEnv *env) 1601 { 1602 if (helperClass == NULL) { 1603 NULL_CHECK0(helperClass = FindBootStrapClass(env, 1604 "sun/launcher/LauncherHelper")); 1605 } 1606 return helperClass; 1607 } 1608 1609 static jmethodID makePlatformStringMID = NULL; 1610 /* 1611 * Returns a new Java string object for the specified platform string. 1612 */ 1613 static jstring 1614 NewPlatformString(JNIEnv *env, char *s) 1615 { 1616 int len = (int)JLI_StrLen(s); 1617 jbyteArray ary; 1618 jclass cls = GetLauncherHelperClass(env); 1619 NULL_CHECK0(cls); 1620 if (s == NULL) 1621 return 0; 1622 1623 ary = (*env)->NewByteArray(env, len); 1624 if (ary != 0) { 1625 jstring str = 0; 1626 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s); 1627 if (!(*env)->ExceptionOccurred(env)) { 1628 if (makePlatformStringMID == NULL) { 1629 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env, 1630 cls, "makePlatformString", "(Z[B)Ljava/lang/String;")); 1631 } 1632 str = (*env)->CallStaticObjectMethod(env, cls, 1633 makePlatformStringMID, USE_STDERR, ary); 1634 CHECK_EXCEPTION_RETURN_VALUE(0); 1635 (*env)->DeleteLocalRef(env, ary); 1636 return str; 1637 } 1638 } 1639 return 0; 1640 } 1641 1642 /* 1643 * Returns a new array of Java string objects for the specified 1644 * array of platform strings. 1645 */ 1646 jobjectArray 1647 NewPlatformStringArray(JNIEnv *env, char **strv, int strc) 1648 { 1649 jarray cls; 1650 jarray ary; 1651 int i; 1652 1653 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String")); 1654 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0)); 1655 CHECK_EXCEPTION_RETURN_VALUE(0); 1656 for (i = 0; i < strc; i++) { 1657 jstring str = NewPlatformString(env, *strv++); 1658 NULL_CHECK0(str); 1659 (*env)->SetObjectArrayElement(env, ary, i, str); 1660 (*env)->DeleteLocalRef(env, str); 1661 } 1662 return ary; 1663 } 1664 1665 /* 1666 * Calls LauncherHelper::checkAndLoadMain to verify that the main class 1667 * is present, it is ok to load the main class and then load the main class. 1668 * For more details refer to the java implementation. 1669 */ 1670 static jclass 1671 LoadMainClass(JNIEnv *env, int mode, char *name) 1672 { 1673 jmethodID mid; 1674 jstring str; 1675 jobject result; 1676 jlong start = 0, end = 0; 1677 jclass cls = GetLauncherHelperClass(env); 1678 NULL_CHECK0(cls); 1679 if (JLI_IsTraceLauncher()) { 1680 start = CurrentTimeMicros(); 1681 } 1682 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, 1683 "checkAndLoadMain", 1684 "(ZILjava/lang/String;)Ljava/lang/Class;")); 1685 1686 NULL_CHECK0(str = NewPlatformString(env, name)); 1687 NULL_CHECK0(result = (*env)->CallStaticObjectMethod(env, cls, mid, 1688 USE_STDERR, mode, str)); 1689 1690 if (JLI_IsTraceLauncher()) { 1691 end = CurrentTimeMicros(); 1692 printf("%ld micro seconds to load main class\n", (long)(end-start)); 1693 printf("----%s----\n", JLDEBUG_ENV_ENTRY); 1694 } 1695 1696 return (jclass)result; 1697 } 1698 1699 static jclass 1700 GetApplicationClass(JNIEnv *env) 1701 { 1702 jmethodID mid; 1703 jclass appClass; 1704 jclass cls = GetLauncherHelperClass(env); 1705 NULL_CHECK0(cls); 1706 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, 1707 "getApplicationClass", 1708 "()Ljava/lang/Class;")); 1709 1710 appClass = (*env)->CallStaticObjectMethod(env, cls, mid); 1711 CHECK_EXCEPTION_RETURN_VALUE(0); 1712 return appClass; 1713 } 1714 1715 static char* expandWildcardOnLongOpt(char* arg) { 1716 char *p, *value; 1717 size_t optLen, valueLen; 1718 p = JLI_StrChr(arg, '='); 1719 1720 if (p == NULL || p[1] == '\0') { 1721 JLI_ReportErrorMessage(ARG_ERROR1, arg); 1722 exit(1); 1723 } 1724 p++; 1725 value = (char *) JLI_WildcardExpandClasspath(p); 1726 if (p == value) { 1727 // no wildcard 1728 return arg; 1729 } 1730 1731 optLen = p - arg; 1732 valueLen = JLI_StrLen(value); 1733 p = JLI_MemAlloc(optLen + valueLen + 1); 1734 memcpy(p, arg, optLen); 1735 memcpy(p + optLen, value, valueLen); 1736 p[optLen + valueLen] = '\0'; 1737 return p; 1738 } 1739 1740 /* 1741 * For tools, convert command line args thus: 1742 * javac -cp foo:foo/"*" -J-ms32m ... 1743 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ... 1744 * 1745 * Takes 4 parameters, and returns the populated arguments 1746 */ 1747 static void 1748 TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv) 1749 { 1750 int argc = *pargc; 1751 char **argv = *pargv; 1752 int nargc = argc + jargc; 1753 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *)); 1754 int i; 1755 1756 *pargc = nargc; 1757 *pargv = nargv; 1758 1759 /* Copy the VM arguments (i.e. prefixed with -J) */ 1760 for (i = 0; i < jargc; i++) { 1761 const char *arg = jargv[i]; 1762 if (arg[0] == '-' && arg[1] == 'J') { 1763 assert(arg[2] != '\0' && "Invalid JAVA_ARGS or EXTRA_JAVA_ARGS defined by build"); 1764 *nargv++ = JLI_StringDup(arg + 2); 1765 } 1766 } 1767 1768 for (i = 0; i < argc; i++) { 1769 char *arg = argv[i]; 1770 if (arg[0] == '-' && arg[1] == 'J') { 1771 if (arg[2] == '\0') { 1772 JLI_ReportErrorMessage(ARG_ERROR3); 1773 exit(1); 1774 } 1775 *nargv++ = arg + 2; 1776 } 1777 } 1778 1779 /* Copy the rest of the arguments */ 1780 for (i = 0; i < jargc ; i++) { 1781 const char *arg = jargv[i]; 1782 if (arg[0] != '-' || arg[1] != 'J') { 1783 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg); 1784 } 1785 } 1786 for (i = 0; i < argc; i++) { 1787 char *arg = argv[i]; 1788 if (arg[0] == '-') { 1789 if (arg[1] == 'J') 1790 continue; 1791 if (IsWildCardEnabled()) { 1792 if (IsClassPathOption(arg) && i < argc - 1) { 1793 *nargv++ = arg; 1794 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]); 1795 i++; 1796 continue; 1797 } 1798 if (JLI_StrCCmp(arg, "--class-path=") == 0) { 1799 *nargv++ = expandWildcardOnLongOpt(arg); 1800 continue; 1801 } 1802 } 1803 } 1804 *nargv++ = arg; 1805 } 1806 *nargv = 0; 1807 } 1808 1809 /* 1810 * For our tools, we try to add 3 VM options: 1811 * -Denv.class.path=<envcp> 1812 * -Dapplication.home=<apphome> 1813 * -Djava.class.path=<appcp> 1814 * <envcp> is the user's setting of CLASSPATH -- for instance the user 1815 * tells javac where to find binary classes through this environment 1816 * variable. Notice that users will be able to compile against our 1817 * tools classes (sun.tools.javac.Main) only if they explicitly add 1818 * tools.jar to CLASSPATH. 1819 * <apphome> is the directory where the application is installed. 1820 * <appcp> is the classpath to where our apps' classfiles are. 1821 */ 1822 static jboolean 1823 AddApplicationOptions(int cpathc, const char **cpathv) 1824 { 1825 char *envcp, *appcp, *apphome; 1826 char home[MAXPATHLEN]; /* application home */ 1827 char separator[] = { PATH_SEPARATOR, '\0' }; 1828 int size, i; 1829 1830 { 1831 const char *s = getenv("CLASSPATH"); 1832 if (s) { 1833 s = (char *) JLI_WildcardExpandClasspath(s); 1834 /* 40 for -Denv.class.path= */ 1835 if (JLI_StrLen(s) + 40 > JLI_StrLen(s)) { // Safeguard from overflow 1836 size_t envcpSize = JLI_StrLen(s) + 40; 1837 envcp = (char *)JLI_MemAlloc(envcpSize); 1838 snprintf(envcp, envcpSize, "-Denv.class.path=%s", s); 1839 AddOption(envcp, NULL); 1840 } 1841 } 1842 } 1843 1844 if (!GetApplicationHome(home, sizeof(home))) { 1845 JLI_ReportErrorMessage(CFG_ERROR5); 1846 return JNI_FALSE; 1847 } 1848 1849 /* 40 for '-Dapplication.home=' */ 1850 size_t apphomeSize = JLI_StrLen(home) + 40; 1851 apphome = (char *)JLI_MemAlloc(apphomeSize); 1852 snprintf(apphome, apphomeSize, "-Dapplication.home=%s", home); 1853 AddOption(apphome, NULL); 1854 1855 /* How big is the application's classpath? */ 1856 if (cpathc > 0) { 1857 size = 40; /* 40: "-Djava.class.path=" */ 1858 for (i = 0; i < cpathc; i++) { 1859 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */ 1860 } 1861 appcp = (char *)JLI_MemAlloc(size + 1); 1862 JLI_StrCpy(appcp, "-Djava.class.path="); 1863 for (i = 0; i < cpathc; i++) { 1864 JLI_StrCat(appcp, home); /* c:\program files\myapp */ 1865 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */ 1866 JLI_StrCat(appcp, separator); /* ; */ 1867 } 1868 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */ 1869 AddOption(appcp, NULL); 1870 } 1871 return JNI_TRUE; 1872 } 1873 1874 /* 1875 * inject the -Dsun.java.command pseudo property into the args structure 1876 * this pseudo property is used in the HotSpot VM to expose the 1877 * Java class name and arguments to the main method to the VM. The 1878 * HotSpot VM uses this pseudo property to store the Java class name 1879 * (or jar file name) and the arguments to the class's main method 1880 * to the instrumentation memory region. The sun.java.command pseudo 1881 * property is not exported by HotSpot to the Java layer. 1882 */ 1883 void 1884 SetJavaCommandLineProp(char *what, int argc, char **argv) 1885 { 1886 1887 int i = 0; 1888 size_t len = 0; 1889 char* javaCommand = NULL; 1890 char* dashDstr = "-Dsun.java.command="; 1891 1892 if (what == NULL) { 1893 /* unexpected, one of these should be set. just return without 1894 * setting the property 1895 */ 1896 return; 1897 } 1898 1899 /* determine the amount of memory to allocate assuming 1900 * the individual components will be space separated 1901 */ 1902 len = JLI_StrLen(what); 1903 for (i = 0; i < argc; i++) { 1904 len += JLI_StrLen(argv[i]) + 1; 1905 } 1906 1907 /* allocate the memory */ 1908 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1); 1909 1910 /* build the -D string */ 1911 *javaCommand = '\0'; 1912 JLI_StrCat(javaCommand, dashDstr); 1913 JLI_StrCat(javaCommand, what); 1914 1915 for (i = 0; i < argc; i++) { 1916 /* the components of the string are space separated. In 1917 * the case of embedded white space, the relationship of 1918 * the white space separated components to their true 1919 * positional arguments will be ambiguous. This issue may 1920 * be addressed in a future release. 1921 */ 1922 JLI_StrCat(javaCommand, " "); 1923 JLI_StrCat(javaCommand, argv[i]); 1924 } 1925 1926 AddOption(javaCommand, NULL); 1927 } 1928 1929 /* 1930 * JVM would like to know if it's created by a standard Sun launcher, or by 1931 * user native application, the following property indicates the former. 1932 */ 1933 static void SetJavaLauncherProp() { 1934 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL); 1935 } 1936 1937 /* 1938 * Prints the version information from the java.version and other properties. 1939 */ 1940 static void 1941 PrintJavaVersion(JNIEnv *env) 1942 { 1943 jclass ver; 1944 jmethodID print; 1945 1946 NULL_CHECK(ver = FindBootStrapClass(env, "java/lang/VersionProps")); 1947 NULL_CHECK(print = (*env)->GetStaticMethodID(env, 1948 ver, 1949 "print", 1950 "(Z)V" 1951 ) 1952 ); 1953 1954 (*env)->CallStaticVoidMethod(env, ver, print, printTo); 1955 } 1956 1957 /* 1958 * Prints all the Java settings, see the java implementation for more details. 1959 */ 1960 static void 1961 ShowSettings(JNIEnv *env, char *optString) 1962 { 1963 jmethodID showSettingsID; 1964 jstring joptString; 1965 jclass cls = GetLauncherHelperClass(env); 1966 NULL_CHECK(cls); 1967 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls, 1968 "showSettings", "(ZLjava/lang/String;JJJ)V")); 1969 NULL_CHECK(joptString = (*env)->NewStringUTF(env, optString)); 1970 (*env)->CallStaticVoidMethod(env, cls, showSettingsID, 1971 USE_STDERR, 1972 joptString, 1973 (jlong)initialHeapSize, 1974 (jlong)maxHeapSize, 1975 (jlong)threadStackSize); 1976 } 1977 1978 /** 1979 * Show resolved modules 1980 */ 1981 static void 1982 ShowResolvedModules(JNIEnv *env) 1983 { 1984 jmethodID showResolvedModulesID; 1985 jclass cls = GetLauncherHelperClass(env); 1986 NULL_CHECK(cls); 1987 NULL_CHECK(showResolvedModulesID = (*env)->GetStaticMethodID(env, cls, 1988 "showResolvedModules", "()V")); 1989 (*env)->CallStaticVoidMethod(env, cls, showResolvedModulesID); 1990 } 1991 1992 /** 1993 * List observable modules 1994 */ 1995 static void 1996 ListModules(JNIEnv *env) 1997 { 1998 jmethodID listModulesID; 1999 jclass cls = GetLauncherHelperClass(env); 2000 NULL_CHECK(cls); 2001 NULL_CHECK(listModulesID = (*env)->GetStaticMethodID(env, cls, 2002 "listModules", "()V")); 2003 (*env)->CallStaticVoidMethod(env, cls, listModulesID); 2004 } 2005 2006 /** 2007 * Describe a module 2008 */ 2009 static void 2010 DescribeModule(JNIEnv *env, char *optString) 2011 { 2012 jmethodID describeModuleID; 2013 jstring joptString = NULL; 2014 jclass cls = GetLauncherHelperClass(env); 2015 NULL_CHECK(cls); 2016 NULL_CHECK(describeModuleID = (*env)->GetStaticMethodID(env, cls, 2017 "describeModule", "(Ljava/lang/String;)V")); 2018 NULL_CHECK(joptString = NewPlatformString(env, optString)); 2019 (*env)->CallStaticVoidMethod(env, cls, describeModuleID, joptString); 2020 } 2021 2022 /* 2023 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java 2024 */ 2025 static void 2026 PrintUsage(JNIEnv* env, jboolean doXUsage) 2027 { 2028 jmethodID initHelp, vmSelect, vmSynonym, printHelp, printXUsageMessage; 2029 jstring jprogname, vm1, vm2; 2030 int i; 2031 jclass cls = GetLauncherHelperClass(env); 2032 NULL_CHECK(cls); 2033 if (doXUsage) { 2034 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls, 2035 "printXUsageMessage", "(Z)V")); 2036 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, printTo); 2037 } else { 2038 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls, 2039 "initHelpMessage", "(Ljava/lang/String;)V")); 2040 2041 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage", 2042 "(Ljava/lang/String;Ljava/lang/String;)V")); 2043 2044 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls, 2045 "appendVmSynonymMessage", 2046 "(Ljava/lang/String;Ljava/lang/String;)V")); 2047 2048 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls, 2049 "printHelpMessage", "(Z)V")); 2050 2051 NULL_CHECK(jprogname = (*env)->NewStringUTF(env, _program_name)); 2052 2053 /* Initialize the usage message with the usual preamble */ 2054 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname); 2055 CHECK_EXCEPTION_RETURN(); 2056 2057 2058 /* Assemble the other variant part of the usage */ 2059 for (i=1; i<knownVMsCount; i++) { 2060 if (knownVMs[i].flag == VM_KNOWN) { 2061 NULL_CHECK(vm1 = (*env)->NewStringUTF(env, knownVMs[i].name)); 2062 NULL_CHECK(vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1)); 2063 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2); 2064 CHECK_EXCEPTION_RETURN(); 2065 } 2066 } 2067 for (i=1; i<knownVMsCount; i++) { 2068 if (knownVMs[i].flag == VM_ALIASED_TO) { 2069 NULL_CHECK(vm1 = (*env)->NewStringUTF(env, knownVMs[i].name)); 2070 NULL_CHECK(vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1)); 2071 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2); 2072 CHECK_EXCEPTION_RETURN(); 2073 } 2074 } 2075 2076 /* Complete the usage message and print to stderr*/ 2077 (*env)->CallStaticVoidMethod(env, cls, printHelp, printTo); 2078 } 2079 return; 2080 } 2081 2082 /* 2083 * Read the jvm.cfg file and fill the knownJVMs[] array. 2084 * 2085 * The functionality of the jvm.cfg file is subject to change without 2086 * notice and the mechanism will be removed in the future. 2087 * 2088 * The lexical structure of the jvm.cfg file is as follows: 2089 * 2090 * jvmcfg := { vmLine } 2091 * vmLine := knownLine 2092 * | aliasLine 2093 * | warnLine 2094 * | ignoreLine 2095 * | errorLine 2096 * | predicateLine 2097 * | commentLine 2098 * knownLine := flag "KNOWN" EOL 2099 * warnLine := flag "WARN" EOL 2100 * ignoreLine := flag "IGNORE" EOL 2101 * errorLine := flag "ERROR" EOL 2102 * aliasLine := flag "ALIASED_TO" flag EOL 2103 * predicateLine := flag "IF_SERVER_CLASS" flag EOL 2104 * commentLine := "#" text EOL 2105 * flag := "-" identifier 2106 * 2107 * The semantics are that when someone specifies a flag on the command line: 2108 * - if the flag appears on a knownLine, then the identifier is used as 2109 * the name of the directory holding the JVM library (the name of the JVM). 2110 * - if the flag appears as the first flag on an aliasLine, the identifier 2111 * of the second flag is used as the name of the JVM. 2112 * - if the flag appears on a warnLine, the identifier is used as the 2113 * name of the JVM, but a warning is generated. 2114 * - if the flag appears on an ignoreLine, the identifier is recognized as the 2115 * name of a JVM, but the identifier is ignored and the default vm used 2116 * - if the flag appears on an errorLine, an error is generated. 2117 * - if the flag appears as the first flag on a predicateLine, and 2118 * the machine on which you are running passes the predicate indicated, 2119 * then the identifier of the second flag is used as the name of the JVM, 2120 * otherwise the identifier of the first flag is used as the name of the JVM. 2121 * If no flag is given on the command line, the first vmLine of the jvm.cfg 2122 * file determines the name of the JVM. 2123 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file, 2124 * since they only make sense if someone hasn't specified the name of the 2125 * JVM on the command line. 2126 * 2127 * The intent of the jvm.cfg file is to allow several JVM libraries to 2128 * be installed in different subdirectories of a single JRE installation, 2129 * for space-savings and convenience in testing. 2130 * The intent is explicitly not to provide a full aliasing or predicate 2131 * mechanism. 2132 */ 2133 jint 2134 ReadKnownVMs(const char *jvmCfgName, jboolean speculative) 2135 { 2136 FILE *jvmCfg; 2137 char line[MAXPATHLEN+20]; 2138 int cnt = 0; 2139 int lineno = 0; 2140 jlong start = 0, end = 0; 2141 int vmType; 2142 char *tmpPtr; 2143 char *altVMName = NULL; 2144 static char *whiteSpace = " \t"; 2145 if (JLI_IsTraceLauncher()) { 2146 start = CurrentTimeMicros(); 2147 } 2148 2149 jvmCfg = fopen(jvmCfgName, "r"); 2150 if (jvmCfg == NULL) { 2151 if (!speculative) { 2152 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName); 2153 exit(1); 2154 } else { 2155 return -1; 2156 } 2157 } 2158 while (fgets(line, sizeof(line), jvmCfg) != NULL) { 2159 vmType = VM_UNKNOWN; 2160 lineno++; 2161 if (line[0] == '#') 2162 continue; 2163 if (line[0] != '-') { 2164 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName); 2165 } 2166 if (cnt >= knownVMsLimit) { 2167 GrowKnownVMs(cnt); 2168 } 2169 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */ 2170 tmpPtr = line + JLI_StrCSpn(line, whiteSpace); 2171 if (*tmpPtr == 0) { 2172 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2173 } else { 2174 /* Null-terminate this string for JLI_StringDup below */ 2175 *tmpPtr++ = 0; 2176 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace); 2177 if (*tmpPtr == 0) { 2178 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2179 } else { 2180 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) { 2181 vmType = VM_KNOWN; 2182 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) { 2183 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace); 2184 if (*tmpPtr != 0) { 2185 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace); 2186 } 2187 if (*tmpPtr == 0) { 2188 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2189 } else { 2190 /* Null terminate altVMName */ 2191 altVMName = tmpPtr; 2192 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace); 2193 *tmpPtr = 0; 2194 vmType = VM_ALIASED_TO; 2195 } 2196 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) { 2197 vmType = VM_WARN; 2198 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) { 2199 vmType = VM_IGNORE; 2200 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) { 2201 vmType = VM_ERROR; 2202 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) { 2203 /* ignored */ 2204 } else { 2205 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]); 2206 vmType = VM_KNOWN; 2207 } 2208 } 2209 } 2210 2211 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line); 2212 if (vmType != VM_UNKNOWN) { 2213 knownVMs[cnt].name = JLI_StringDup(line); 2214 knownVMs[cnt].flag = vmType; 2215 switch (vmType) { 2216 default: 2217 break; 2218 case VM_ALIASED_TO: 2219 knownVMs[cnt].alias = JLI_StringDup(altVMName); 2220 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n", 2221 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias); 2222 break; 2223 } 2224 cnt++; 2225 } 2226 } 2227 fclose(jvmCfg); 2228 knownVMsCount = cnt; 2229 2230 if (JLI_IsTraceLauncher()) { 2231 end = CurrentTimeMicros(); 2232 printf("%ld micro seconds to parse jvm.cfg\n", (long)(end-start)); 2233 } 2234 2235 return cnt; 2236 } 2237 2238 2239 static void 2240 GrowKnownVMs(int minimum) 2241 { 2242 struct vmdesc* newKnownVMs; 2243 int newMax; 2244 2245 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit)); 2246 if (newMax <= minimum) { 2247 newMax = minimum; 2248 } 2249 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc)); 2250 if (knownVMs != NULL) { 2251 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc)); 2252 } 2253 JLI_MemFree(knownVMs); 2254 knownVMs = newKnownVMs; 2255 knownVMsLimit = newMax; 2256 } 2257 2258 2259 /* Returns index of VM or -1 if not found */ 2260 static int 2261 KnownVMIndex(const char* name) 2262 { 2263 int i; 2264 if (JLI_StrCCmp(name, "-J") == 0) name += 2; 2265 for (i = 0; i < knownVMsCount; i++) { 2266 if (!JLI_StrCmp(name, knownVMs[i].name)) { 2267 return i; 2268 } 2269 } 2270 return -1; 2271 } 2272 2273 static void 2274 FreeKnownVMs() 2275 { 2276 int i; 2277 for (i = 0; i < knownVMsCount; i++) { 2278 JLI_MemFree(knownVMs[i].name); 2279 knownVMs[i].name = NULL; 2280 } 2281 JLI_MemFree(knownVMs); 2282 } 2283 2284 /* 2285 * Displays the splash screen according to the jar file name 2286 * and image file names stored in environment variables 2287 */ 2288 void 2289 ShowSplashScreen() 2290 { 2291 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY); 2292 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY); 2293 int data_size; 2294 void *image_data = NULL; 2295 float scale_factor = 1; 2296 char *scaled_splash_name = NULL; 2297 jboolean isImageScaled = JNI_FALSE; 2298 size_t maxScaledImgNameLength = 0; 2299 if (file_name == NULL){ 2300 return; 2301 } 2302 2303 if (!DoSplashInit()) { 2304 goto exit; 2305 } 2306 2307 maxScaledImgNameLength = DoSplashGetScaledImgNameMaxPstfixLen(file_name); 2308 2309 scaled_splash_name = JLI_MemAlloc( 2310 maxScaledImgNameLength * sizeof(char)); 2311 isImageScaled = DoSplashGetScaledImageName(jar_name, file_name, 2312 &scale_factor, 2313 scaled_splash_name, maxScaledImgNameLength); 2314 if (jar_name) { 2315 2316 if (isImageScaled) { 2317 image_data = JLI_JarUnpackFile( 2318 jar_name, scaled_splash_name, &data_size); 2319 } 2320 2321 if (!image_data) { 2322 scale_factor = 1; 2323 image_data = JLI_JarUnpackFile( 2324 jar_name, file_name, &data_size); 2325 } 2326 if (image_data) { 2327 DoSplashSetScaleFactor(scale_factor); 2328 DoSplashLoadMemory(image_data, data_size); 2329 JLI_MemFree(image_data); 2330 } else { 2331 DoSplashClose(); 2332 } 2333 } else { 2334 if (isImageScaled) { 2335 DoSplashSetScaleFactor(scale_factor); 2336 DoSplashLoadFile(scaled_splash_name); 2337 } else { 2338 DoSplashLoadFile(file_name); 2339 } 2340 } 2341 JLI_MemFree(scaled_splash_name); 2342 2343 DoSplashSetFileJarName(file_name, jar_name); 2344 2345 exit: 2346 /* 2347 * Done with all command line processing and potential re-execs so 2348 * clean up the environment. 2349 */ 2350 (void)UnsetEnv(ENV_ENTRY); 2351 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY); 2352 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY); 2353 2354 JLI_MemFree(splash_jar_entry); 2355 JLI_MemFree(splash_file_entry); 2356 2357 } 2358 2359 static const char* GetFullVersion() 2360 { 2361 return _fVersion; 2362 } 2363 2364 static const char* GetProgramName() 2365 { 2366 return _program_name; 2367 } 2368 2369 static const char* GetLauncherName() 2370 { 2371 return _launcher_name; 2372 } 2373 2374 static jboolean IsJavaArgs() 2375 { 2376 return _is_java_args; 2377 } 2378 2379 static jboolean 2380 IsWildCardEnabled() 2381 { 2382 return _wc_enabled; 2383 } 2384 2385 int 2386 ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize, 2387 int argc, char **argv, 2388 int mode, char *what, int ret) 2389 { 2390 if (threadStackSize == 0) { 2391 /* 2392 * If the user hasn't specified a non-zero stack size ask the JVM for its default. 2393 * A returned 0 means 'use the system default' for a platform, e.g., Windows. 2394 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will 2395 * return its default stack size through the init args structure. 2396 */ 2397 struct JDK1_1InitArgs args1_1; 2398 memset((void*)&args1_1, 0, sizeof(args1_1)); 2399 args1_1.version = JNI_VERSION_1_1; 2400 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */ 2401 if (args1_1.javaStackSize > 0) { 2402 threadStackSize = args1_1.javaStackSize; 2403 } 2404 } 2405 2406 { /* Create a new thread to create JVM and invoke main method */ 2407 JavaMainArgs args; 2408 int rslt; 2409 2410 args.argc = argc; 2411 args.argv = argv; 2412 args.mode = mode; 2413 args.what = what; 2414 args.ifn = *ifn; 2415 2416 rslt = CallJavaMainInNewThread(threadStackSize, (void*)&args); 2417 /* If the caller has deemed there is an error we 2418 * simply return that, otherwise we return the value of 2419 * the callee 2420 */ 2421 return (ret != 0) ? ret : rslt; 2422 } 2423 } 2424 2425 static void 2426 DumpState() 2427 { 2428 if (!JLI_IsTraceLauncher()) return ; 2429 printf("Launcher state:\n"); 2430 printf("\tFirst application arg index: %d\n", JLI_GetAppArgIndex()); 2431 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off"); 2432 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off"); 2433 printf("\tprogram name:%s\n", GetProgramName()); 2434 printf("\tlauncher name:%s\n", GetLauncherName()); 2435 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off"); 2436 printf("\tfullversion:%s\n", GetFullVersion()); 2437 } 2438 2439 /* 2440 * A utility procedure to always print to stderr 2441 */ 2442 JNIEXPORT void JNICALL 2443 JLI_ReportMessage(const char* fmt, ...) 2444 { 2445 va_list vl; 2446 va_start(vl, fmt); 2447 vfprintf(stderr, fmt, vl); 2448 fprintf(stderr, "\n"); 2449 va_end(vl); 2450 } 2451 2452 /* 2453 * A utility procedure to always print to stdout 2454 */ 2455 void 2456 JLI_ShowMessage(const char* fmt, ...) 2457 { 2458 va_list vl; 2459 va_start(vl, fmt); 2460 vfprintf(stdout, fmt, vl); 2461 fprintf(stdout, "\n"); 2462 va_end(vl); 2463 }