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