1 /* 2 * Copyright (c) 1995, 2024, 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 if (JLI_StrCmp(arg, "-XX:AOTMode=create") == 0) { 1522 // Alias for -Xshare:dump 1523 dumpSharedSpaces = JNI_TRUE; 1524 } 1525 } 1526 1527 if (*pwhat == NULL && --argc >= 0) { 1528 *pwhat = *argv++; 1529 } 1530 1531 if (*pwhat == NULL) { 1532 /* LM_UNKNOWN okay for options that exit */ 1533 if (!listModules && !describeModule && !validateModules && !dumpSharedSpaces) { 1534 *pret = 1; 1535 } 1536 } else if (mode == LM_UNKNOWN) { 1537 /* default to LM_CLASS if -m, -jar and -cp options are 1538 * not specified */ 1539 if (!_have_classpath) { 1540 SetClassPath("."); 1541 } 1542 mode = IsSourceFile(arg) ? LM_SOURCE : LM_CLASS; 1543 } else if (mode == LM_CLASS && IsSourceFile(arg)) { 1544 /* override LM_CLASS mode if given a source file */ 1545 mode = LM_SOURCE; 1546 } 1547 1548 if (mode == LM_SOURCE) { 1549 AddOption("--add-modules=ALL-DEFAULT", NULL); 1550 *pwhat = SOURCE_LAUNCHER_MAIN_ENTRY; 1551 // adjust (argc, argv) so that the name of the source file 1552 // is included in the args passed to the source launcher 1553 // main entry class 1554 *pargc = argc + 1; 1555 *pargv = argv - 1; 1556 } else { 1557 if (argc >= 0) { 1558 *pargc = argc; 1559 *pargv = argv; 1560 } 1561 } 1562 1563 *pmode = mode; 1564 1565 return JNI_TRUE; 1566 } 1567 1568 /* 1569 * Initializes the Java Virtual Machine. Also frees options array when 1570 * finished. 1571 */ 1572 static jboolean 1573 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn) 1574 { 1575 JavaVMInitArgs args; 1576 jint r; 1577 1578 memset(&args, 0, sizeof(args)); 1579 args.version = JNI_VERSION_1_2; 1580 args.nOptions = numOptions; 1581 args.options = options; 1582 args.ignoreUnrecognized = JNI_FALSE; 1583 1584 if (JLI_IsTraceLauncher()) { 1585 int i = 0; 1586 printf("JavaVM args:\n "); 1587 printf("version 0x%08lx, ", (long)args.version); 1588 printf("ignoreUnrecognized is %s, ", 1589 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE"); 1590 printf("nOptions is %ld\n", (long)args.nOptions); 1591 for (i = 0; i < numOptions; i++) 1592 printf(" option[%2d] = '%s'\n", 1593 i, args.options[i].optionString); 1594 } 1595 1596 r = ifn->CreateJavaVM(pvm, (void **)penv, &args); 1597 JLI_MemFree(options); 1598 return r == JNI_OK; 1599 } 1600 1601 static jclass helperClass = NULL; 1602 1603 jclass 1604 GetLauncherHelperClass(JNIEnv *env) 1605 { 1606 if (helperClass == NULL) { 1607 NULL_CHECK0(helperClass = FindBootStrapClass(env, 1608 "sun/launcher/LauncherHelper")); 1609 } 1610 return helperClass; 1611 } 1612 1613 static jmethodID makePlatformStringMID = NULL; 1614 /* 1615 * Returns a new Java string object for the specified platform string. 1616 */ 1617 static jstring 1618 NewPlatformString(JNIEnv *env, char *s) 1619 { 1620 int len = (int)JLI_StrLen(s); 1621 jbyteArray ary; 1622 jclass cls = GetLauncherHelperClass(env); 1623 NULL_CHECK0(cls); 1624 if (s == NULL) 1625 return 0; 1626 1627 ary = (*env)->NewByteArray(env, len); 1628 if (ary != 0) { 1629 jstring str = 0; 1630 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s); 1631 if (!(*env)->ExceptionOccurred(env)) { 1632 if (makePlatformStringMID == NULL) { 1633 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env, 1634 cls, "makePlatformString", "(Z[B)Ljava/lang/String;")); 1635 } 1636 str = (*env)->CallStaticObjectMethod(env, cls, 1637 makePlatformStringMID, USE_STDERR, ary); 1638 CHECK_EXCEPTION_RETURN_VALUE(0); 1639 (*env)->DeleteLocalRef(env, ary); 1640 return str; 1641 } 1642 } 1643 return 0; 1644 } 1645 1646 /* 1647 * Returns a new array of Java string objects for the specified 1648 * array of platform strings. 1649 */ 1650 jobjectArray 1651 NewPlatformStringArray(JNIEnv *env, char **strv, int strc) 1652 { 1653 jarray cls; 1654 jarray ary; 1655 int i; 1656 1657 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String")); 1658 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0)); 1659 CHECK_EXCEPTION_RETURN_VALUE(0); 1660 for (i = 0; i < strc; i++) { 1661 jstring str = NewPlatformString(env, *strv++); 1662 NULL_CHECK0(str); 1663 (*env)->SetObjectArrayElement(env, ary, i, str); 1664 (*env)->DeleteLocalRef(env, str); 1665 } 1666 return ary; 1667 } 1668 1669 /* 1670 * Calls LauncherHelper::checkAndLoadMain to verify that the main class 1671 * is present, it is ok to load the main class and then load the main class. 1672 * For more details refer to the java implementation. 1673 */ 1674 static jclass 1675 LoadMainClass(JNIEnv *env, int mode, char *name) 1676 { 1677 jmethodID mid; 1678 jstring str; 1679 jobject result; 1680 jlong start = 0, end = 0; 1681 jclass cls = GetLauncherHelperClass(env); 1682 NULL_CHECK0(cls); 1683 if (JLI_IsTraceLauncher()) { 1684 start = CurrentTimeMicros(); 1685 } 1686 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, 1687 "checkAndLoadMain", 1688 "(ZILjava/lang/String;)Ljava/lang/Class;")); 1689 1690 NULL_CHECK0(str = NewPlatformString(env, name)); 1691 NULL_CHECK0(result = (*env)->CallStaticObjectMethod(env, cls, mid, 1692 USE_STDERR, mode, str)); 1693 1694 if (JLI_IsTraceLauncher()) { 1695 end = CurrentTimeMicros(); 1696 printf("%ld micro seconds to load main class\n", (long)(end-start)); 1697 printf("----%s----\n", JLDEBUG_ENV_ENTRY); 1698 } 1699 1700 return (jclass)result; 1701 } 1702 1703 static jclass 1704 GetApplicationClass(JNIEnv *env) 1705 { 1706 jmethodID mid; 1707 jclass appClass; 1708 jclass cls = GetLauncherHelperClass(env); 1709 NULL_CHECK0(cls); 1710 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, 1711 "getApplicationClass", 1712 "()Ljava/lang/Class;")); 1713 1714 appClass = (*env)->CallStaticObjectMethod(env, cls, mid); 1715 CHECK_EXCEPTION_RETURN_VALUE(0); 1716 return appClass; 1717 } 1718 1719 static char* expandWildcardOnLongOpt(char* arg) { 1720 char *p, *value; 1721 size_t optLen, valueLen; 1722 p = JLI_StrChr(arg, '='); 1723 1724 if (p == NULL || p[1] == '\0') { 1725 JLI_ReportErrorMessage(ARG_ERROR1, arg); 1726 exit(1); 1727 } 1728 p++; 1729 value = (char *) JLI_WildcardExpandClasspath(p); 1730 if (p == value) { 1731 // no wildcard 1732 return arg; 1733 } 1734 1735 optLen = p - arg; 1736 valueLen = JLI_StrLen(value); 1737 p = JLI_MemAlloc(optLen + valueLen + 1); 1738 memcpy(p, arg, optLen); 1739 memcpy(p + optLen, value, valueLen); 1740 p[optLen + valueLen] = '\0'; 1741 return p; 1742 } 1743 1744 /* 1745 * For tools, convert command line args thus: 1746 * javac -cp foo:foo/"*" -J-ms32m ... 1747 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ... 1748 * 1749 * Takes 4 parameters, and returns the populated arguments 1750 */ 1751 static void 1752 TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv) 1753 { 1754 int argc = *pargc; 1755 char **argv = *pargv; 1756 int nargc = argc + jargc; 1757 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *)); 1758 int i; 1759 1760 *pargc = nargc; 1761 *pargv = nargv; 1762 1763 /* Copy the VM arguments (i.e. prefixed with -J) */ 1764 for (i = 0; i < jargc; i++) { 1765 const char *arg = jargv[i]; 1766 if (arg[0] == '-' && arg[1] == 'J') { 1767 assert(arg[2] != '\0' && "Invalid JAVA_ARGS or EXTRA_JAVA_ARGS defined by build"); 1768 *nargv++ = JLI_StringDup(arg + 2); 1769 } 1770 } 1771 1772 for (i = 0; i < argc; i++) { 1773 char *arg = argv[i]; 1774 if (arg[0] == '-' && arg[1] == 'J') { 1775 if (arg[2] == '\0') { 1776 JLI_ReportErrorMessage(ARG_ERROR3); 1777 exit(1); 1778 } 1779 *nargv++ = arg + 2; 1780 } 1781 } 1782 1783 /* Copy the rest of the arguments */ 1784 for (i = 0; i < jargc ; i++) { 1785 const char *arg = jargv[i]; 1786 if (arg[0] != '-' || arg[1] != 'J') { 1787 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg); 1788 } 1789 } 1790 for (i = 0; i < argc; i++) { 1791 char *arg = argv[i]; 1792 if (arg[0] == '-') { 1793 if (arg[1] == 'J') 1794 continue; 1795 if (IsWildCardEnabled()) { 1796 if (IsClassPathOption(arg) && i < argc - 1) { 1797 *nargv++ = arg; 1798 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]); 1799 i++; 1800 continue; 1801 } 1802 if (JLI_StrCCmp(arg, "--class-path=") == 0) { 1803 *nargv++ = expandWildcardOnLongOpt(arg); 1804 continue; 1805 } 1806 } 1807 } 1808 *nargv++ = arg; 1809 } 1810 *nargv = 0; 1811 } 1812 1813 /* 1814 * For our tools, we try to add 3 VM options: 1815 * -Denv.class.path=<envcp> 1816 * -Dapplication.home=<apphome> 1817 * -Djava.class.path=<appcp> 1818 * <envcp> is the user's setting of CLASSPATH -- for instance the user 1819 * tells javac where to find binary classes through this environment 1820 * variable. Notice that users will be able to compile against our 1821 * tools classes (sun.tools.javac.Main) only if they explicitly add 1822 * tools.jar to CLASSPATH. 1823 * <apphome> is the directory where the application is installed. 1824 * <appcp> is the classpath to where our apps' classfiles are. 1825 */ 1826 static jboolean 1827 AddApplicationOptions(int cpathc, const char **cpathv) 1828 { 1829 char *envcp, *appcp, *apphome; 1830 char home[MAXPATHLEN]; /* application home */ 1831 char separator[] = { PATH_SEPARATOR, '\0' }; 1832 int size, i; 1833 1834 { 1835 const char *s = getenv("CLASSPATH"); 1836 if (s) { 1837 s = (char *) JLI_WildcardExpandClasspath(s); 1838 /* 40 for -Denv.class.path= */ 1839 if (JLI_StrLen(s) + 40 > JLI_StrLen(s)) { // Safeguard from overflow 1840 size_t envcpSize = JLI_StrLen(s) + 40; 1841 envcp = (char *)JLI_MemAlloc(envcpSize); 1842 snprintf(envcp, envcpSize, "-Denv.class.path=%s", s); 1843 AddOption(envcp, NULL); 1844 } 1845 } 1846 } 1847 1848 if (!GetApplicationHome(home, sizeof(home))) { 1849 JLI_ReportErrorMessage(CFG_ERROR5); 1850 return JNI_FALSE; 1851 } 1852 1853 /* 40 for '-Dapplication.home=' */ 1854 size_t apphomeSize = JLI_StrLen(home) + 40; 1855 apphome = (char *)JLI_MemAlloc(apphomeSize); 1856 snprintf(apphome, apphomeSize, "-Dapplication.home=%s", home); 1857 AddOption(apphome, NULL); 1858 1859 /* How big is the application's classpath? */ 1860 if (cpathc > 0) { 1861 size = 40; /* 40: "-Djava.class.path=" */ 1862 for (i = 0; i < cpathc; i++) { 1863 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */ 1864 } 1865 appcp = (char *)JLI_MemAlloc(size + 1); 1866 JLI_StrCpy(appcp, "-Djava.class.path="); 1867 for (i = 0; i < cpathc; i++) { 1868 JLI_StrCat(appcp, home); /* c:\program files\myapp */ 1869 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */ 1870 JLI_StrCat(appcp, separator); /* ; */ 1871 } 1872 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */ 1873 AddOption(appcp, NULL); 1874 } 1875 return JNI_TRUE; 1876 } 1877 1878 /* 1879 * inject the -Dsun.java.command pseudo property into the args structure 1880 * this pseudo property is used in the HotSpot VM to expose the 1881 * Java class name and arguments to the main method to the VM. The 1882 * HotSpot VM uses this pseudo property to store the Java class name 1883 * (or jar file name) and the arguments to the class's main method 1884 * to the instrumentation memory region. The sun.java.command pseudo 1885 * property is not exported by HotSpot to the Java layer. 1886 */ 1887 void 1888 SetJavaCommandLineProp(char *what, int argc, char **argv) 1889 { 1890 1891 int i = 0; 1892 size_t len = 0; 1893 char* javaCommand = NULL; 1894 char* dashDstr = "-Dsun.java.command="; 1895 1896 if (what == NULL) { 1897 /* unexpected, one of these should be set. just return without 1898 * setting the property 1899 */ 1900 return; 1901 } 1902 1903 /* determine the amount of memory to allocate assuming 1904 * the individual components will be space separated 1905 */ 1906 len = JLI_StrLen(what); 1907 for (i = 0; i < argc; i++) { 1908 len += JLI_StrLen(argv[i]) + 1; 1909 } 1910 1911 /* allocate the memory */ 1912 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1); 1913 1914 /* build the -D string */ 1915 *javaCommand = '\0'; 1916 JLI_StrCat(javaCommand, dashDstr); 1917 JLI_StrCat(javaCommand, what); 1918 1919 for (i = 0; i < argc; i++) { 1920 /* the components of the string are space separated. In 1921 * the case of embedded white space, the relationship of 1922 * the white space separated components to their true 1923 * positional arguments will be ambiguous. This issue may 1924 * be addressed in a future release. 1925 */ 1926 JLI_StrCat(javaCommand, " "); 1927 JLI_StrCat(javaCommand, argv[i]); 1928 } 1929 1930 AddOption(javaCommand, NULL); 1931 } 1932 1933 /* 1934 * JVM would like to know if it's created by a standard Sun launcher, or by 1935 * user native application, the following property indicates the former. 1936 */ 1937 static void SetJavaLauncherProp() { 1938 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL); 1939 } 1940 1941 /* 1942 * Prints the version information from the java.version and other properties. 1943 */ 1944 static void 1945 PrintJavaVersion(JNIEnv *env) 1946 { 1947 jclass ver; 1948 jmethodID print; 1949 1950 NULL_CHECK(ver = FindBootStrapClass(env, "java/lang/VersionProps")); 1951 NULL_CHECK(print = (*env)->GetStaticMethodID(env, 1952 ver, 1953 "print", 1954 "(Z)V" 1955 ) 1956 ); 1957 1958 (*env)->CallStaticVoidMethod(env, ver, print, printTo); 1959 } 1960 1961 /* 1962 * Prints all the Java settings, see the java implementation for more details. 1963 */ 1964 static void 1965 ShowSettings(JNIEnv *env, char *optString) 1966 { 1967 jmethodID showSettingsID; 1968 jstring joptString; 1969 jclass cls = GetLauncherHelperClass(env); 1970 NULL_CHECK(cls); 1971 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls, 1972 "showSettings", "(ZLjava/lang/String;JJJ)V")); 1973 NULL_CHECK(joptString = (*env)->NewStringUTF(env, optString)); 1974 (*env)->CallStaticVoidMethod(env, cls, showSettingsID, 1975 USE_STDERR, 1976 joptString, 1977 (jlong)initialHeapSize, 1978 (jlong)maxHeapSize, 1979 (jlong)threadStackSize); 1980 } 1981 1982 /** 1983 * Show resolved modules 1984 */ 1985 static void 1986 ShowResolvedModules(JNIEnv *env) 1987 { 1988 jmethodID showResolvedModulesID; 1989 jclass cls = GetLauncherHelperClass(env); 1990 NULL_CHECK(cls); 1991 NULL_CHECK(showResolvedModulesID = (*env)->GetStaticMethodID(env, cls, 1992 "showResolvedModules", "()V")); 1993 (*env)->CallStaticVoidMethod(env, cls, showResolvedModulesID); 1994 } 1995 1996 /** 1997 * List observable modules 1998 */ 1999 static void 2000 ListModules(JNIEnv *env) 2001 { 2002 jmethodID listModulesID; 2003 jclass cls = GetLauncherHelperClass(env); 2004 NULL_CHECK(cls); 2005 NULL_CHECK(listModulesID = (*env)->GetStaticMethodID(env, cls, 2006 "listModules", "()V")); 2007 (*env)->CallStaticVoidMethod(env, cls, listModulesID); 2008 } 2009 2010 /** 2011 * Describe a module 2012 */ 2013 static void 2014 DescribeModule(JNIEnv *env, char *optString) 2015 { 2016 jmethodID describeModuleID; 2017 jstring joptString = NULL; 2018 jclass cls = GetLauncherHelperClass(env); 2019 NULL_CHECK(cls); 2020 NULL_CHECK(describeModuleID = (*env)->GetStaticMethodID(env, cls, 2021 "describeModule", "(Ljava/lang/String;)V")); 2022 NULL_CHECK(joptString = NewPlatformString(env, optString)); 2023 (*env)->CallStaticVoidMethod(env, cls, describeModuleID, joptString); 2024 } 2025 2026 /* 2027 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java 2028 */ 2029 static void 2030 PrintUsage(JNIEnv* env, jboolean doXUsage) 2031 { 2032 jmethodID initHelp, vmSelect, vmSynonym, printHelp, printXUsageMessage; 2033 jstring jprogname, vm1, vm2; 2034 int i; 2035 jclass cls = GetLauncherHelperClass(env); 2036 NULL_CHECK(cls); 2037 if (doXUsage) { 2038 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls, 2039 "printXUsageMessage", "(Z)V")); 2040 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, printTo); 2041 } else { 2042 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls, 2043 "initHelpMessage", "(Ljava/lang/String;)V")); 2044 2045 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage", 2046 "(Ljava/lang/String;Ljava/lang/String;)V")); 2047 2048 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls, 2049 "appendVmSynonymMessage", 2050 "(Ljava/lang/String;Ljava/lang/String;)V")); 2051 2052 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls, 2053 "printHelpMessage", "(Z)V")); 2054 2055 NULL_CHECK(jprogname = (*env)->NewStringUTF(env, _program_name)); 2056 2057 /* Initialize the usage message with the usual preamble */ 2058 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname); 2059 CHECK_EXCEPTION_RETURN(); 2060 2061 2062 /* Assemble the other variant part of the usage */ 2063 for (i=1; i<knownVMsCount; i++) { 2064 if (knownVMs[i].flag == VM_KNOWN) { 2065 NULL_CHECK(vm1 = (*env)->NewStringUTF(env, knownVMs[i].name)); 2066 NULL_CHECK(vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1)); 2067 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2); 2068 CHECK_EXCEPTION_RETURN(); 2069 } 2070 } 2071 for (i=1; i<knownVMsCount; i++) { 2072 if (knownVMs[i].flag == VM_ALIASED_TO) { 2073 NULL_CHECK(vm1 = (*env)->NewStringUTF(env, knownVMs[i].name)); 2074 NULL_CHECK(vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1)); 2075 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2); 2076 CHECK_EXCEPTION_RETURN(); 2077 } 2078 } 2079 2080 /* Complete the usage message and print to stderr*/ 2081 (*env)->CallStaticVoidMethod(env, cls, printHelp, printTo); 2082 } 2083 return; 2084 } 2085 2086 /* 2087 * Read the jvm.cfg file and fill the knownJVMs[] array. 2088 * 2089 * The functionality of the jvm.cfg file is subject to change without 2090 * notice and the mechanism will be removed in the future. 2091 * 2092 * The lexical structure of the jvm.cfg file is as follows: 2093 * 2094 * jvmcfg := { vmLine } 2095 * vmLine := knownLine 2096 * | aliasLine 2097 * | warnLine 2098 * | ignoreLine 2099 * | errorLine 2100 * | predicateLine 2101 * | commentLine 2102 * knownLine := flag "KNOWN" EOL 2103 * warnLine := flag "WARN" EOL 2104 * ignoreLine := flag "IGNORE" EOL 2105 * errorLine := flag "ERROR" EOL 2106 * aliasLine := flag "ALIASED_TO" flag EOL 2107 * predicateLine := flag "IF_SERVER_CLASS" flag EOL 2108 * commentLine := "#" text EOL 2109 * flag := "-" identifier 2110 * 2111 * The semantics are that when someone specifies a flag on the command line: 2112 * - if the flag appears on a knownLine, then the identifier is used as 2113 * the name of the directory holding the JVM library (the name of the JVM). 2114 * - if the flag appears as the first flag on an aliasLine, the identifier 2115 * of the second flag is used as the name of the JVM. 2116 * - if the flag appears on a warnLine, the identifier is used as the 2117 * name of the JVM, but a warning is generated. 2118 * - if the flag appears on an ignoreLine, the identifier is recognized as the 2119 * name of a JVM, but the identifier is ignored and the default vm used 2120 * - if the flag appears on an errorLine, an error is generated. 2121 * - if the flag appears as the first flag on a predicateLine, and 2122 * the machine on which you are running passes the predicate indicated, 2123 * then the identifier of the second flag is used as the name of the JVM, 2124 * otherwise the identifier of the first flag is used as the name of the JVM. 2125 * If no flag is given on the command line, the first vmLine of the jvm.cfg 2126 * file determines the name of the JVM. 2127 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file, 2128 * since they only make sense if someone hasn't specified the name of the 2129 * JVM on the command line. 2130 * 2131 * The intent of the jvm.cfg file is to allow several JVM libraries to 2132 * be installed in different subdirectories of a single JRE installation, 2133 * for space-savings and convenience in testing. 2134 * The intent is explicitly not to provide a full aliasing or predicate 2135 * mechanism. 2136 */ 2137 jint 2138 ReadKnownVMs(const char *jvmCfgName, jboolean speculative) 2139 { 2140 FILE *jvmCfg; 2141 char line[MAXPATHLEN+20]; 2142 int cnt = 0; 2143 int lineno = 0; 2144 jlong start = 0, end = 0; 2145 int vmType; 2146 char *tmpPtr; 2147 char *altVMName = NULL; 2148 static char *whiteSpace = " \t"; 2149 if (JLI_IsTraceLauncher()) { 2150 start = CurrentTimeMicros(); 2151 } 2152 2153 jvmCfg = fopen(jvmCfgName, "r"); 2154 if (jvmCfg == NULL) { 2155 if (!speculative) { 2156 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName); 2157 exit(1); 2158 } else { 2159 return -1; 2160 } 2161 } 2162 while (fgets(line, sizeof(line), jvmCfg) != NULL) { 2163 vmType = VM_UNKNOWN; 2164 lineno++; 2165 if (line[0] == '#') 2166 continue; 2167 if (line[0] != '-') { 2168 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName); 2169 } 2170 if (cnt >= knownVMsLimit) { 2171 GrowKnownVMs(cnt); 2172 } 2173 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */ 2174 tmpPtr = line + JLI_StrCSpn(line, whiteSpace); 2175 if (*tmpPtr == 0) { 2176 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2177 } else { 2178 /* Null-terminate this string for JLI_StringDup below */ 2179 *tmpPtr++ = 0; 2180 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace); 2181 if (*tmpPtr == 0) { 2182 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2183 } else { 2184 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) { 2185 vmType = VM_KNOWN; 2186 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) { 2187 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace); 2188 if (*tmpPtr != 0) { 2189 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace); 2190 } 2191 if (*tmpPtr == 0) { 2192 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName); 2193 } else { 2194 /* Null terminate altVMName */ 2195 altVMName = tmpPtr; 2196 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace); 2197 *tmpPtr = 0; 2198 vmType = VM_ALIASED_TO; 2199 } 2200 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) { 2201 vmType = VM_WARN; 2202 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) { 2203 vmType = VM_IGNORE; 2204 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) { 2205 vmType = VM_ERROR; 2206 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) { 2207 /* ignored */ 2208 } else { 2209 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]); 2210 vmType = VM_KNOWN; 2211 } 2212 } 2213 } 2214 2215 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line); 2216 if (vmType != VM_UNKNOWN) { 2217 knownVMs[cnt].name = JLI_StringDup(line); 2218 knownVMs[cnt].flag = vmType; 2219 switch (vmType) { 2220 default: 2221 break; 2222 case VM_ALIASED_TO: 2223 knownVMs[cnt].alias = JLI_StringDup(altVMName); 2224 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n", 2225 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias); 2226 break; 2227 } 2228 cnt++; 2229 } 2230 } 2231 fclose(jvmCfg); 2232 knownVMsCount = cnt; 2233 2234 if (JLI_IsTraceLauncher()) { 2235 end = CurrentTimeMicros(); 2236 printf("%ld micro seconds to parse jvm.cfg\n", (long)(end-start)); 2237 } 2238 2239 return cnt; 2240 } 2241 2242 2243 static void 2244 GrowKnownVMs(int minimum) 2245 { 2246 struct vmdesc* newKnownVMs; 2247 int newMax; 2248 2249 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit)); 2250 if (newMax <= minimum) { 2251 newMax = minimum; 2252 } 2253 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc)); 2254 if (knownVMs != NULL) { 2255 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc)); 2256 } 2257 JLI_MemFree(knownVMs); 2258 knownVMs = newKnownVMs; 2259 knownVMsLimit = newMax; 2260 } 2261 2262 2263 /* Returns index of VM or -1 if not found */ 2264 static int 2265 KnownVMIndex(const char* name) 2266 { 2267 int i; 2268 if (JLI_StrCCmp(name, "-J") == 0) name += 2; 2269 for (i = 0; i < knownVMsCount; i++) { 2270 if (!JLI_StrCmp(name, knownVMs[i].name)) { 2271 return i; 2272 } 2273 } 2274 return -1; 2275 } 2276 2277 static void 2278 FreeKnownVMs() 2279 { 2280 int i; 2281 for (i = 0; i < knownVMsCount; i++) { 2282 JLI_MemFree(knownVMs[i].name); 2283 knownVMs[i].name = NULL; 2284 } 2285 JLI_MemFree(knownVMs); 2286 } 2287 2288 /* 2289 * Displays the splash screen according to the jar file name 2290 * and image file names stored in environment variables 2291 */ 2292 void 2293 ShowSplashScreen() 2294 { 2295 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY); 2296 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY); 2297 int data_size; 2298 void *image_data = NULL; 2299 float scale_factor = 1; 2300 char *scaled_splash_name = NULL; 2301 jboolean isImageScaled = JNI_FALSE; 2302 size_t maxScaledImgNameLength = 0; 2303 if (file_name == NULL){ 2304 return; 2305 } 2306 2307 if (!DoSplashInit()) { 2308 goto exit; 2309 } 2310 2311 maxScaledImgNameLength = DoSplashGetScaledImgNameMaxPstfixLen(file_name); 2312 2313 scaled_splash_name = JLI_MemAlloc( 2314 maxScaledImgNameLength * sizeof(char)); 2315 isImageScaled = DoSplashGetScaledImageName(jar_name, file_name, 2316 &scale_factor, 2317 scaled_splash_name, maxScaledImgNameLength); 2318 if (jar_name) { 2319 2320 if (isImageScaled) { 2321 image_data = JLI_JarUnpackFile( 2322 jar_name, scaled_splash_name, &data_size); 2323 } 2324 2325 if (!image_data) { 2326 scale_factor = 1; 2327 image_data = JLI_JarUnpackFile( 2328 jar_name, file_name, &data_size); 2329 } 2330 if (image_data) { 2331 DoSplashSetScaleFactor(scale_factor); 2332 DoSplashLoadMemory(image_data, data_size); 2333 JLI_MemFree(image_data); 2334 } else { 2335 DoSplashClose(); 2336 } 2337 } else { 2338 if (isImageScaled) { 2339 DoSplashSetScaleFactor(scale_factor); 2340 DoSplashLoadFile(scaled_splash_name); 2341 } else { 2342 DoSplashLoadFile(file_name); 2343 } 2344 } 2345 JLI_MemFree(scaled_splash_name); 2346 2347 DoSplashSetFileJarName(file_name, jar_name); 2348 2349 exit: 2350 /* 2351 * Done with all command line processing and potential re-execs so 2352 * clean up the environment. 2353 */ 2354 (void)UnsetEnv(ENV_ENTRY); 2355 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY); 2356 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY); 2357 2358 JLI_MemFree(splash_jar_entry); 2359 JLI_MemFree(splash_file_entry); 2360 2361 } 2362 2363 static const char* GetFullVersion() 2364 { 2365 return _fVersion; 2366 } 2367 2368 static const char* GetProgramName() 2369 { 2370 return _program_name; 2371 } 2372 2373 static const char* GetLauncherName() 2374 { 2375 return _launcher_name; 2376 } 2377 2378 static jboolean IsJavaArgs() 2379 { 2380 return _is_java_args; 2381 } 2382 2383 static jboolean 2384 IsWildCardEnabled() 2385 { 2386 return _wc_enabled; 2387 } 2388 2389 int 2390 ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize, 2391 int argc, char **argv, 2392 int mode, char *what, int ret) 2393 { 2394 if (threadStackSize == 0) { 2395 /* 2396 * If the user hasn't specified a non-zero stack size ask the JVM for its default. 2397 * A returned 0 means 'use the system default' for a platform, e.g., Windows. 2398 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will 2399 * return its default stack size through the init args structure. 2400 */ 2401 struct JDK1_1InitArgs args1_1; 2402 memset((void*)&args1_1, 0, sizeof(args1_1)); 2403 args1_1.version = JNI_VERSION_1_1; 2404 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */ 2405 if (args1_1.javaStackSize > 0) { 2406 threadStackSize = args1_1.javaStackSize; 2407 } 2408 } 2409 2410 { /* Create a new thread to create JVM and invoke main method */ 2411 JavaMainArgs args; 2412 int rslt; 2413 2414 args.argc = argc; 2415 args.argv = argv; 2416 args.mode = mode; 2417 args.what = what; 2418 args.ifn = *ifn; 2419 2420 rslt = CallJavaMainInNewThread(threadStackSize, (void*)&args); 2421 /* If the caller has deemed there is an error we 2422 * simply return that, otherwise we return the value of 2423 * the callee 2424 */ 2425 return (ret != 0) ? ret : rslt; 2426 } 2427 } 2428 2429 static void 2430 DumpState() 2431 { 2432 if (!JLI_IsTraceLauncher()) return ; 2433 printf("Launcher state:\n"); 2434 printf("\tFirst application arg index: %d\n", JLI_GetAppArgIndex()); 2435 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off"); 2436 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off"); 2437 printf("\tprogram name:%s\n", GetProgramName()); 2438 printf("\tlauncher name:%s\n", GetLauncherName()); 2439 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off"); 2440 printf("\tfullversion:%s\n", GetFullVersion()); 2441 } 2442 2443 /* 2444 * A utility procedure to always print to stderr 2445 */ 2446 JNIEXPORT void JNICALL 2447 JLI_ReportMessage(const char* fmt, ...) 2448 { 2449 va_list vl; 2450 va_start(vl, fmt); 2451 vfprintf(stderr, fmt, vl); 2452 fprintf(stderr, "\n"); 2453 va_end(vl); 2454 } 2455 2456 /* 2457 * A utility procedure to always print to stdout 2458 */ 2459 void 2460 JLI_ShowMessage(const char* fmt, ...) 2461 { 2462 va_list vl; 2463 va_start(vl, fmt); 2464 vfprintf(stdout, fmt, vl); 2465 fprintf(stdout, "\n"); 2466 va_end(vl); 2467 }