2 * Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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).
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.
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Shared source for 'java' command line tool.
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.
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. On unix platforms, the
46 * CreateExecutionEnvironment function from the unix java_md.c file
47 * processes and removes -d<n> options. However, in case
48 * CreateExecutionEnvironment does not need to exec because
49 * LD_LIBRARY_PATH is set acceptably and the data model does not need
50 * to be changed, ParseArguments will screen out the redundant -d<n>
51 * options and prevent them from being passed to the vm; this is done
52 * by using the machine-dependent call
53 * RemovableMachineDependentOption.
63 #include "manifest_info.h"
64 #include "version_comp.h"
66 #include "splashscreen.h"
69 #define FULL_VERSION JDK_MAJOR_VERSION "." JDK_MINOR_VERSION
73 * The following environment variable is used to influence the behavior
74 * of the jre exec'd through the SelectVersion routine. The command line
75 * options which specify the version are not passed to the exec'd version,
76 * because that jre may be an older version which wouldn't recognize them.
77 * This environment variable is known to this (and later) version and serves
78 * to suppress the version selection code. This is not only for efficiency,
79 * but also for correctness, since any command line options have been
80 * removed which would cause any value found in the manifest to be used.
81 * This would be incorrect because the command line options are defined
84 * The value associated with this environment variable is the MainClass
85 * name from within the executable jar file (if any). This is strictly a
86 * performance enhancement to avoid re-reading the jar file manifest.
88 * A NOTE TO DEVELOPERS: For performance reasons it is important that
89 * the program image remain relatively small until after SelectVersion
90 * CreateExecutionEnvironment have finished their possibly recursive
91 * processing. Watch everything, but resist all temptations to use Java
94 #define ENV_ENTRY "_JAVA_VERSION_SET"
96 #define SPLASH_FILE_ENV_ENTRY "_JAVA_SPLASH_FILE"
97 #define SPLASH_JAR_ENV_ENTRY "_JAVA_SPLASH_JAR"
99 static jboolean printVersion = JNI_FALSE; /* print and exit */
100 static jboolean showVersion = JNI_FALSE; /* print but continue */
101 static jboolean printUsage = JNI_FALSE; /* print and exit*/
102 static jboolean printXUsage = JNI_FALSE; /* print and exit*/
103 static char *progname;
104 static char *launchername;
105 jboolean _launcher_debug = JNI_FALSE;
108 * Entries for splash screen environment variables.
109 * putenv is performed in SelectVersion. We need
110 * them in memory until UnsetEnv, so they are made static
111 * global instead of auto local.
113 static char* splash_file_entry = NULL;
114 static char* splash_jar_entry = NULL;
117 * List of VM options to be specified when the VM is created.
119 static JavaVMOption *options;
120 static int numOptions, maxOptions;
123 * Prototypes for functions internal to launcher.
125 static void SetClassPath(const char *s);
126 static void SelectVersion(int argc, char **argv, char **main_class);
127 static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
128 char **pclassname, int *pret, const char *jvmpath);
129 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
130 InvocationFunctions *ifn);
131 static jstring NewPlatformString(JNIEnv *env, char *s);
132 static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
133 static jclass LoadClass(JNIEnv *env, char *name);
134 static jstring GetMainClassName(JNIEnv *env, char *jarname);
135 static void SetJavaCommandLineProp(char* classname, char* jarfile, int argc, char** argv);
136 static void SetJavaLauncherProp(void);
139 static void TranslateApplicationArgs(int *pargc, char ***pargv);
140 static jboolean AddApplicationOptions(void);
143 static void PrintJavaVersion(JNIEnv *env);
144 static void PrintUsage(JNIEnv* env, jboolean doXUsage);
146 static void SetPaths(int argc, char **argv);
149 /* Maximum supported entries from jvm.cfg. */
150 #define INIT_MAX_KNOWN_VMS 10
151 /* Values for vmdesc.flag */
152 #define VM_UNKNOWN -1
154 #define VM_ALIASED_TO 1
157 #define VM_IF_SERVER_CLASS 4
165 static struct vmdesc *knownVMs = NULL;
166 static int knownVMsCount = 0;
167 static int knownVMsLimit = 0;
169 static void GrowKnownVMs();
170 static int KnownVMIndex(const char* name);
171 static void FreeKnownVMs();
172 static void ShowSplashScreen();
174 jboolean ServerClassMachine();
176 /* flag which if set suppresses error messages from the launcher */
177 static int noExitErrorMessage = 0;
180 * Running Java code in primordial thread caused many problems. We will
181 * create a new thread to invoke JVM. See 6316197 for more information.
183 static jlong threadStackSize = 0; /* stack size of the new thread */
185 int JNICALL JavaMain(void * args); /* entry point */
187 struct JavaMainArgs {
192 InvocationFunctions ifn;
199 main(int argc, char ** argv)
204 char *main_class = NULL;
206 InvocationFunctions ifn;
208 char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];
209 char ** original_argv = argv;
211 if (getenv("_JAVA_LAUNCHER_DEBUG") != 0) {
213 _launcher_debug = JNI_TRUE;
214 printf("----_JAVA_LAUNCHER_DEBUG----\n");
215 printf("Command line Args:\n");
216 for(i = 0; i < argc+1; i++) {
217 if (argv[i] != NULL){
218 printf("\targv[%d] = '%s'\n",i,argv[i]);
226 * Make sure the specified version of the JRE is running.
228 * There are three things to note about the SelectVersion() routine:
229 * 1) If the version running isn't correct, this routine doesn't
230 * return (either the correct version has been exec'd or an error
232 * 2) Argc and Argv in this scope are *not* altered by this routine.
233 * It is the responsibility of subsequent code to ignore the
234 * arguments handled by this routine.
235 * 3) As a side-effect, the variable "main_class" is guaranteed to
236 * be set (if it should ever be set). This isn't exactly the
237 * poster child for structured programming, but it is a small
238 * price to pay for not processing a jar file operand twice.
239 * (Note: This side effect has been disabled. See comment on
240 * bugid 5030265 below.)
242 SelectVersion(argc, argv, &main_class);
244 /* copy original argv */
247 original_argv = (char**)JLI_MemAlloc(sizeof(char*)*(argc+1));
248 for(i = 0; i < argc+1; i++) {
249 original_argv[i] = argv[i];
253 CreateExecutionEnvironment(&argc, &argv,
254 jrepath, sizeof(jrepath),
255 jvmpath, sizeof(jvmpath),
258 ifn.CreateJavaVM = 0;
259 ifn.GetDefaultJavaVMInitArgs = 0;
262 start = CounterGet();
263 if (!LoadJavaVM(jvmpath, &ifn)) {
266 if (_launcher_debug) {
268 printf("%ld micro seconds to LoadJavaVM\n",
269 (long)(jint)Counter2Micros(end-start));
272 #ifdef JAVA_ARGS /* javac, jar and friends. */
274 #else /* java, oldjava, javaw and friends */
279 if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
282 #endif /* PROGNAME */
283 #endif /* JAVA_ARGS */
286 launchername = LAUNCHER_NAME;
288 launchername = progname;
289 #endif /* LAUNCHER_NAME */
295 /* Preprocess wrapper arguments */
296 TranslateApplicationArgs(&argc, &argv);
297 if (!AddApplicationOptions()) {
302 /* Set default CLASSPATH */
303 if ((s = getenv("CLASSPATH")) == 0) {
311 * Parse command line options; if the return value of
312 * ParseArguments is false, the program should exit.
314 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
318 /* Override class path if -jar flag was specified */
320 SetClassPath(jarfile);
323 /* set the -Dsun.java.command pseudo property */
324 SetJavaCommandLineProp(classname, jarfile, argc, argv);
326 /* Set the -Dsun.java.launcher pseudo property */
327 SetJavaLauncherProp();
329 /* set the -Dsun.java.launcher.* platform properties */
330 SetJavaLauncherPlatformProps();
332 /* Show the splash screen if needed */
336 * Done with all command line processing and potential re-execs so
337 * clean up the environment.
339 (void)UnsetEnv(ENV_ENTRY);
340 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
341 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
343 JLI_MemFree(splash_jar_entry);
344 JLI_MemFree(splash_file_entry);
347 * If user doesn't specify stack size, check if VM has a preference.
348 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
349 * return its default stack size through the init args structure.
351 if (threadStackSize == 0) {
352 struct JDK1_1InitArgs args1_1;
353 memset((void*)&args1_1, 0, sizeof(args1_1));
354 args1_1.version = JNI_VERSION_1_1;
355 ifn.GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
356 if (args1_1.javaStackSize > 0) {
357 threadStackSize = args1_1.javaStackSize;
361 { /* Create a new thread to create JVM and invoke main method */
362 struct JavaMainArgs args;
366 args.jarfile = jarfile;
367 args.classname = classname;
370 return ContinueInNewThread(JavaMain, threadStackSize, (void*)&args, ret);
375 JavaMain(void * _args)
377 struct JavaMainArgs *args = (struct JavaMainArgs *)_args;
378 int argc = args->argc;
379 char **argv = args->argv;
380 char *jarfile = args->jarfile;
381 char *classname = args->classname;
382 InvocationFunctions ifn = args->ifn;
386 jstring mainClassName;
389 jobjectArray mainArgs;
394 * Error message to print or display; by default the message will
395 * only be displayed in a window.
397 char * message = "Fatal exception occurred. Program will exit.";
398 jboolean messageDest = JNI_FALSE;
400 /* Initialize the virtual machine */
403 start = CounterGet();
404 if (!InitializeJVM(&vm, &env, &ifn)) {
405 ReportErrorMessage("Could not create the Java virtual machine.",
410 if (printVersion || showVersion) {
411 PrintJavaVersion(env);
412 if ((*env)->ExceptionOccurred(env)) {
413 ReportExceptionDescription(env);
422 fprintf(stderr, "\n");
426 /* If the user specified neither a class name nor a JAR file */
427 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
428 PrintUsage(env, printXUsage);
429 if ((*env)->ExceptionOccurred(env)) {
430 ReportExceptionDescription(env);
439 FreeKnownVMs(); /* after last possible PrintUsage() */
441 if (_launcher_debug) {
443 printf("%ld micro seconds to InitializeJVM\n",
444 (long)(jint)Counter2Micros(end-start));
447 /* At this stage, argc/argv have the applications' arguments */
448 if (_launcher_debug) {
450 printf("Main-Class is '%s'\n", classname ? classname : "");
451 printf("Apps' argc is %d\n", argc);
452 for (; i < argc; i++) {
453 printf(" argv[%2d] = '%s'\n", i, argv[i]);
460 * Get the application's main class.
462 * See bugid 5030265. The Main-Class name has already been parsed
463 * from the manifest, but not parsed properly for UTF-8 support.
464 * Hence the code here ignores the value previously extracted and
465 * uses the pre-existing code to reextract the value. This is
466 * possibly an end of release cycle expedient. However, it has
467 * also been discovered that passing some character sets through
468 * the environment has "strange" behavior on some variants of
469 * Windows. Hence, maybe the manifest parsing code local to the
470 * launcher should never be enhanced.
472 * Hence, future work should either:
473 * 1) Correct the local parsing code and verify that the
474 * Main-Class attribute gets properly passed through
476 * 2) Remove the vestages of maintaining main_class through
477 * the environment (and remove these comments).
480 mainClassName = GetMainClassName(env, jarfile);
481 if ((*env)->ExceptionOccurred(env)) {
482 ReportExceptionDescription(env);
485 if (mainClassName == NULL) {
486 const char * format = "Failed to load Main-Class manifest "
487 "attribute from\n%s";
488 message = (char*)JLI_MemAlloc((strlen(format) + strlen(jarfile)) *
490 sprintf(message, format, jarfile);
491 messageDest = JNI_TRUE;
494 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
495 if (classname == NULL) {
496 ReportExceptionDescription(env);
499 mainClass = LoadClass(env, classname);
500 if(mainClass == NULL) { /* exception occured */
501 const char * format = "Could not find the main class: %s. Program will exit.";
502 ReportExceptionDescription(env);
503 message = (char *)JLI_MemAlloc((strlen(format) +
504 strlen(classname)) * sizeof(char));
505 messageDest = JNI_TRUE;
506 sprintf(message, format, classname);
509 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
511 mainClassName = NewPlatformString(env, classname);
512 if (mainClassName == NULL) {
513 const char * format = "Failed to load Main Class: %s";
514 message = (char *)JLI_MemAlloc((strlen(format) + strlen(classname)) *
516 sprintf(message, format, classname);
517 messageDest = JNI_TRUE;
520 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
521 if (classname == NULL) {
522 ReportExceptionDescription(env);
525 mainClass = LoadClass(env, classname);
526 if(mainClass == NULL) { /* exception occured */
527 const char * format = "Could not find the main class: %s. Program will exit.";
528 ReportExceptionDescription(env);
529 message = (char *)JLI_MemAlloc((strlen(format) +
530 strlen(classname)) * sizeof(char));
531 messageDest = JNI_TRUE;
532 sprintf(message, format, classname);
535 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
538 /* Get the application's main method */
539 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
540 "([Ljava/lang/String;)V");
541 if (mainID == NULL) {
542 if ((*env)->ExceptionOccurred(env)) {
543 ReportExceptionDescription(env);
545 message = "No main method found in specified class.";
546 messageDest = JNI_TRUE;
551 { /* Make sure the main method is public */
554 jobject obj = (*env)->ToReflectedMethod(env, mainClass,
557 if( obj == NULL) { /* exception occurred */
558 ReportExceptionDescription(env);
563 (*env)->GetMethodID(env,
564 (*env)->GetObjectClass(env, obj),
565 "getModifiers", "()I");
566 if ((*env)->ExceptionOccurred(env)) {
567 ReportExceptionDescription(env);
571 mods = (*env)->CallIntMethod(env, obj, mid);
572 if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
573 message = "Main method not public.";
574 messageDest = JNI_TRUE;
579 /* Build argument array */
580 mainArgs = NewPlatformStringArray(env, argv, argc);
581 if (mainArgs == NULL) {
582 ReportExceptionDescription(env);
586 /* Invoke main method. */
587 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
590 * The launcher's exit code (in the absence of calls to
591 * System.exit) will be non-zero if main threw an exception.
593 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
596 * Detach the main thread so that it appears to have ended when
597 * the application's main method exits. This will invoke the
598 * uncaught exception handler machinery if main threw an
599 * exception. An uncaught exception handler cannot change the
600 * launcher's return code except by calling System.exit.
602 if ((*vm)->DetachCurrentThread(vm) != 0) {
603 message = "Could not detach main thread.";
604 messageDest = JNI_TRUE;
613 * Wait for all non-daemon threads to end, then destroy the VM.
614 * This will actually create a trivial new Java waiter thread
615 * named "DestroyJavaVM", but this will be seen as a different
616 * thread from the one that executed main, even though they are
617 * the same C thread. This allows mainThread.join() and
618 * mainThread.isAlive() to work as expected.
620 (*vm)->DestroyJavaVM(vm);
622 if(message != NULL && !noExitErrorMessage)
623 ReportErrorMessage(message, messageDest);
629 * Checks the command line options to find which JVM type was
630 * specified. If no command line option was given for the JVM type,
631 * the default type is used. The environment variable
632 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
633 * checked as ways of specifying which JVM type to invoke.
636 CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
643 char *jvmtype = getenv("JDK_ALTERNATE_VM");
647 /* To make things simpler we always copy the argv array */
648 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
650 /* The program name is always present */
651 newArgv[newArgvIdx++] = (*argv)[0];
653 for (argi = 1; argi < argc; argi++) {
654 char *arg = (*argv)[argi];
659 newArgv[newArgvIdx++] = arg;
663 if (strcmp(arg, "-classpath") == 0 ||
664 strcmp(arg, "-cp") == 0) {
665 newArgv[newArgvIdx++] = arg;
668 newArgv[newArgvIdx++] = (*argv)[argi];
672 if (arg[0] != '-') break;
675 /* Did the user pass an explicit VM type? */
676 i = KnownVMIndex(arg);
678 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
683 /* Did the user specify an "alternate" VM? */
684 else if (strncmp(arg, "-XXaltjvm=", 10) == 0 || strncmp(arg, "-J-XXaltjvm=", 12) == 0) {
686 jvmtype = arg+((arg[1]=='X')? 10 : 12);
691 newArgv[newArgvIdx++] = arg;
696 * Finish copying the arguments if we aborted the above loop.
697 * NOTE that if we aborted via "break" then we did NOT copy the
698 * last argument above, and in addition argi will be less than
701 while (argi < argc) {
702 newArgv[newArgvIdx++] = (*argv)[argi];
706 /* argv is null-terminated */
707 newArgv[newArgvIdx] = 0;
713 /* use the default VM type if not specified (no alias processing) */
714 if (jvmtype == NULL) {
715 char* result = knownVMs[0].name+1;
716 /* Use a different VM type if we are on a server class machine? */
717 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
718 (ServerClassMachine() == JNI_TRUE)) {
719 result = knownVMs[0].server_class+1;
721 if (_launcher_debug) {
722 printf("Default VM: %s\n", result);
727 /* if using an alternate VM, no alias processing */
731 /* Resolve aliases first */
734 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
735 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
737 if (loopCount > knownVMsCount) {
739 ReportErrorMessage("Error: Corrupt jvm.cfg file; cycle in alias list.",
750 ReportErrorMessage2("Error: Unable to resolve VM alias %s",
751 knownVMs[jvmidx].alias, JNI_TRUE);
758 jvmtype = knownVMs[jvmidx].name+1;
763 switch (knownVMs[jvmidx].flag) {
766 fprintf(stderr, "Warning: %s VM not supported; %s VM will be used\n",
767 jvmtype, knownVMs[0].name + 1);
771 jvmtype = knownVMs[jvmidx=0].name + 1;
777 ReportErrorMessage2("Error: %s VM not supported", jvmtype, JNI_TRUE);
788 # define MB (1024UL * KB)
789 # define GB (1024UL * MB)
791 /* copied from HotSpot function "atomll()" */
793 parse_stack_size(const char *s, jlong *result) {
795 int args_read = sscanf(s, jlong_format_specifier(), &n);
796 if (args_read != 1) {
799 while (*s != '\0' && *s >= '0' && *s <= '9') {
802 // 4705540: illegal if more characters are found after the first non-digit
808 *result = n * GB * KB;
823 /* Create JVM with default stack and let VM handle malformed -Xss string*/
829 * Adds a new VM option with the given given name and value.
832 AddOption(char *str, void *info)
835 * Expand options array if needed to accommodate at least one more
838 if (numOptions >= maxOptions) {
841 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
845 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
846 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
847 JLI_MemFree(options);
851 options[numOptions].optionString = str;
852 options[numOptions++].extraInfo = info;
854 if (strncmp(str, "-Xss", 4) == 0) {
856 if (parse_stack_size(str + 4, &tmp)) {
857 threadStackSize = tmp;
863 SetClassPath(const char *s)
866 s = JLI_WildcardExpandClasspath(s);
867 def = JLI_MemAlloc(strlen(s) + 40);
868 sprintf(def, "-Djava.class.path=%s", s);
869 AddOption(def, NULL);
873 * The SelectVersion() routine ensures that an appropriate version of
874 * the JRE is running. The specification for the appropriate version
875 * is obtained from either the manifest of a jar file (preferred) or
876 * from command line options.
877 * The routine also parses splash screen command line options and
878 * passes on their values in private environment variables.
881 SelectVersion(int argc, char **argv, char **main_class)
887 char *version = NULL;
890 int headlessflag = 0;
891 int restrict_search = -1; /* -1 implies not known */
893 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
894 char *splash_file_name = NULL;
895 char *splash_jar_name = NULL;
900 * If the version has already been selected, set *main_class
901 * with the value passed through the environment (if any) and
904 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
906 *main_class = JLI_StringDup(env_in);
911 * Scan through the arguments for options relevant to multiple JRE
912 * support. For reference, the command line syntax is defined as:
915 * java [options] class [argument...]
917 * java [options] -jar file.jar [argument...]
919 * As the scan is performed, make a copy of the argument list with
920 * the version specification options (new to 1.5) removed, so that
921 * a version less than 1.5 can be exec'd.
923 * Note that due to the syntax of the native Windows interface
924 * CreateProcess(), processing similar to the following exists in
925 * the Windows platform specific routine ExecJRE (in java_md.c).
926 * Changes here should be reproduced there.
928 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
929 new_argv[0] = argv[0];
930 new_argp = &new_argv[1];
933 while ((arg = *argv) != 0 && *arg == '-') {
934 if (strncmp(arg, "-version:", 9) == 0) {
936 } else if (strcmp(arg, "-jre-restrict-search") == 0) {
938 } else if (strcmp(arg, "-no-jre-restrict-search") == 0) {
941 if (strcmp(arg, "-jar") == 0)
943 /* deal with "unfortunate" classpath syntax */
944 if ((strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) &&
953 * Checking for headless toolkit option in the some way as AWT does:
954 * "true" means true and any other value means false
956 if (strcmp(arg, "-Djava.awt.headless=true") == 0) {
958 } else if (strncmp(arg, "-Djava.awt.headless=", 20) == 0) {
960 } else if (strncmp(arg, "-splash:", 8) == 0) {
961 splash_file_name = arg+8;
968 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
972 *new_argp++ = operand = *argv++;
974 while (argc-- > 0) /* Copy over [argument...] */
975 *new_argp++ = *argv++;
979 * If there is a jar file, read the manifest. If the jarfile can't be
980 * read, the manifest can't be read from the jar file, or the manifest
981 * is corrupt, issue the appropriate error messages and exit.
983 * Even if there isn't a jar file, construct a manifest_info structure
984 * containing the command line information. It's a convenient way to carry
987 if (jarflag && operand) {
988 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
990 ReportErrorMessage2("Unable to access jarfile %s",
993 ReportErrorMessage2("Invalid or corrupt jarfile %s",
999 * Command line splash screen option should have precedence
1000 * over the manifest, so the manifest data is used only if
1001 * splash_file_name has not been initialized above during command
1004 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
1005 splash_file_name = info.splashscreen_image_file_name;
1006 splash_jar_name = operand;
1009 info.manifest_version = NULL;
1010 info.main_class = NULL;
1011 info.jre_version = NULL;
1012 info.jre_restrict_search = 0;
1016 * Passing on splash screen info in environment variables
1018 if (splash_file_name && !headlessflag) {
1019 char* splash_file_entry = JLI_MemAlloc(strlen(SPLASH_FILE_ENV_ENTRY "=")+strlen(splash_file_name)+1);
1020 strcpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
1021 strcat(splash_file_entry, splash_file_name);
1022 putenv(splash_file_entry);
1024 if (splash_jar_name && !headlessflag) {
1025 char* splash_jar_entry = JLI_MemAlloc(strlen(SPLASH_JAR_ENV_ENTRY "=")+strlen(splash_jar_name)+1);
1026 strcpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
1027 strcat(splash_jar_entry, splash_jar_name);
1028 putenv(splash_jar_entry);
1032 * The JRE-Version and JRE-Restrict-Search values (if any) from the
1033 * manifest are overwritten by any specified on the command line.
1035 if (version != NULL)
1036 info.jre_version = version;
1037 if (restrict_search != -1)
1038 info.jre_restrict_search = restrict_search;
1041 * "Valid" returns (other than unrecoverable errors) follow. Set
1042 * main_class as a side-effect of this routine.
1044 if (info.main_class != NULL)
1045 *main_class = JLI_StringDup(info.main_class);
1048 * If no version selection information is found either on the command
1049 * line or in the manifest, simply return.
1051 if (info.jre_version == NULL) {
1053 JLI_MemFree(new_argv);
1058 * Check for correct syntax of the version specification (JSR 56).
1060 if (!JLI_ValidVersionString(info.jre_version)) {
1061 ReportErrorMessage2("Syntax error in version specification \"%s\"",
1062 info.jre_version, JNI_TRUE);
1067 * Find the appropriate JVM on the system. Just to be as forgiving as
1068 * possible, if the standard algorithms don't locate an appropriate
1069 * jre, check to see if the one running will satisfy the requirements.
1070 * This can happen on systems which haven't been set-up for multiple
1073 jre = LocateJRE(&info);
1074 if (_launcher_debug)
1075 printf("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
1076 (info.jre_version?info.jre_version:"null"),
1077 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
1079 if (JLI_AcceptableRelease(FULL_VERSION, info.jre_version)) {
1081 JLI_MemFree(new_argv);
1084 ReportErrorMessage2(
1085 "Unable to locate JRE meeting specification \"%s\"",
1086 info.jre_version, JNI_TRUE);
1092 * If I'm not the chosen one, exec the chosen one. Returning from
1093 * ExecJRE indicates that I am indeed the chosen one.
1095 * The private environment variable _JAVA_VERSION_SET is used to
1096 * prevent the chosen one from re-reading the manifest file and
1097 * using the values found within to override the (potential) command
1098 * line flags stripped from argv (because the target may not
1099 * understand them). Passing the MainClass value is an optimization
1100 * to avoid locating, expanding and parsing the manifest extra
1103 if (info.main_class != NULL) {
1104 if (strlen(info.main_class) <= MAXNAMELEN) {
1105 (void)strcat(env_entry, info.main_class);
1107 ReportErrorMessage("Error: main-class: attribute exceeds system limits\n", JNI_TRUE);
1111 (void)putenv(env_entry);
1112 ExecJRE(jre, new_argv);
1114 JLI_MemFree(new_argv);
1119 * Parses command line arguments. Returns JNI_FALSE if launcher
1120 * should exit without starting vm, returns JNI_TRUE if vm needs
1121 * to be started to process given options. *pret (the launcher
1122 * process return value) is set to 0 for a normal exit.
1125 ParseArguments(int *pargc, char ***pargv, char **pjarfile,
1126 char **pclassname, int *pret, const char *jvmpath)
1129 char **argv = *pargv;
1130 jboolean jarflag = JNI_FALSE;
1135 while ((arg = *argv) != 0 && *arg == '-') {
1137 if (strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) {
1139 ReportErrorMessage2("%s requires class path specification",
1141 printUsage = JNI_TRUE;
1145 SetClassPath(*argv);
1147 } else if (strcmp(arg, "-jar") == 0) {
1149 } else if (strcmp(arg, "-help") == 0 ||
1150 strcmp(arg, "-h") == 0 ||
1151 strcmp(arg, "-?") == 0) {
1152 printUsage = JNI_TRUE;
1154 } else if (strcmp(arg, "-version") == 0) {
1155 printVersion = JNI_TRUE;
1157 } else if (strcmp(arg, "-showversion") == 0) {
1158 showVersion = JNI_TRUE;
1159 } else if (strcmp(arg, "-X") == 0) {
1160 printXUsage = JNI_TRUE;
1163 * The following case provide backward compatibility with old-style
1164 * command line options.
1166 } else if (strcmp(arg, "-fullversion") == 0) {
1167 fprintf(stderr, "%s full version \"%s\"\n", launchername,
1170 } else if (strcmp(arg, "-verbosegc") == 0) {
1171 AddOption("-verbose:gc", NULL);
1172 } else if (strcmp(arg, "-t") == 0) {
1173 AddOption("-Xt", NULL);
1174 } else if (strcmp(arg, "-tm") == 0) {
1175 AddOption("-Xtm", NULL);
1176 } else if (strcmp(arg, "-debug") == 0) {
1177 AddOption("-Xdebug", NULL);
1178 } else if (strcmp(arg, "-noclassgc") == 0) {
1179 AddOption("-Xnoclassgc", NULL);
1180 } else if (strcmp(arg, "-Xfuture") == 0) {
1181 AddOption("-Xverify:all", NULL);
1182 } else if (strcmp(arg, "-verify") == 0) {
1183 AddOption("-Xverify:all", NULL);
1184 } else if (strcmp(arg, "-verifyremote") == 0) {
1185 AddOption("-Xverify:remote", NULL);
1186 } else if (strcmp(arg, "-noverify") == 0) {
1187 AddOption("-Xverify:none", NULL);
1188 } else if (strcmp(arg, "-XXsuppressExitMessage") == 0) {
1189 noExitErrorMessage = 1;
1190 } else if (strncmp(arg, "-prof", 5) == 0) {
1192 char *tmp = JLI_MemAlloc(strlen(arg) + 50);
1194 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1196 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1198 AddOption(tmp, NULL);
1199 } else if (strncmp(arg, "-ss", 3) == 0 ||
1200 strncmp(arg, "-oss", 4) == 0 ||
1201 strncmp(arg, "-ms", 3) == 0 ||
1202 strncmp(arg, "-mx", 3) == 0) {
1203 char *tmp = JLI_MemAlloc(strlen(arg) + 6);
1204 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1205 AddOption(tmp, NULL);
1206 } else if (strcmp(arg, "-checksource") == 0 ||
1207 strcmp(arg, "-cs") == 0 ||
1208 strcmp(arg, "-noasyncgc") == 0) {
1209 /* No longer supported */
1211 "Warning: %s option is no longer supported.\n",
1213 } else if (strncmp(arg, "-version:", 9) == 0 ||
1214 strcmp(arg, "-no-jre-restrict-search") == 0 ||
1215 strcmp(arg, "-jre-restrict-search") == 0 ||
1216 strncmp(arg, "-splash:", 8) == 0) {
1217 ; /* Ignore machine independent options already handled */
1218 } else if (RemovableMachineDependentOption(arg) ) {
1219 ; /* Do not pass option to vm. */
1222 AddOption(arg, NULL);
1228 *pjarfile = *argv++;
1232 *pclassname = *argv++;
1242 * Initializes the Java Virtual Machine. Also frees options array when
1246 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1248 JavaVMInitArgs args;
1251 memset(&args, 0, sizeof(args));
1252 args.version = JNI_VERSION_1_2;
1253 args.nOptions = numOptions;
1254 args.options = options;
1255 args.ignoreUnrecognized = JNI_FALSE;
1257 if (_launcher_debug) {
1259 printf("JavaVM args:\n ");
1260 printf("version 0x%08lx, ", (long)args.version);
1261 printf("ignoreUnrecognized is %s, ",
1262 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1263 printf("nOptions is %ld\n", (long)args.nOptions);
1264 for (i = 0; i < numOptions; i++)
1265 printf(" option[%2d] = '%s'\n",
1266 i, args.options[i].optionString);
1269 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1270 JLI_MemFree(options);
1274 #define JNI_ERROR "Error: A JNI error has occurred, please check your installation and try again"
1276 #define NULL_CHECK0(e) if ((e) == 0) { \
1277 ReportErrorMessage(JNI_ERROR, JNI_TRUE); \
1281 #define NULL_CHECK(e) if ((e) == 0) { \
1282 ReportErrorMessage(JNI_ERROR, JNI_TRUE); \
1286 static jstring platformEncoding = NULL;
1287 static jstring getPlatformEncoding(JNIEnv *env) {
1288 if (platformEncoding == NULL) {
1289 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1293 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/lang/System"));
1294 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1297 "(Ljava/lang/String;)Ljava/lang/String;"));
1298 platformEncoding = (*env)->CallStaticObjectMethod (
1299 env, cls, mid, propname);
1302 return platformEncoding;
1305 static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1308 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/nio/charset/Charset"));
1309 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1312 "(Ljava/lang/String;)Z"));
1313 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1317 * Returns a new Java string object for the specified platform string.
1320 NewPlatformString(JNIEnv *env, char *s)
1322 int len = (int)strlen(s);
1330 enc = getPlatformEncoding(env);
1332 ary = (*env)->NewByteArray(env, len);
1335 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1336 if (!(*env)->ExceptionOccurred(env)) {
1337 if (isEncodingSupported(env, enc) == JNI_TRUE) {
1338 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1339 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1340 "([BLjava/lang/String;)V"));
1341 str = (*env)->NewObject(env, cls, mid, ary, enc);
1343 /*If the encoding specified in sun.jnu.encoding is not
1344 endorsed by "Charset.isSupported" we have to fall back
1345 to use String(byte[]) explicitly here without specifying
1346 the encoding name, in which the StringCoding class will
1347 pickup the iso-8859-1 as the fallback converter for us.
1349 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1350 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1352 str = (*env)->NewObject(env, cls, mid, ary);
1354 (*env)->DeleteLocalRef(env, ary);
1362 * Returns a new array of Java string objects for the specified
1363 * array of platform strings.
1366 NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1372 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1373 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1374 for (i = 0; i < strc; i++) {
1375 jstring str = NewPlatformString(env, *strv++);
1377 (*env)->SetObjectArrayElement(env, ary, i, str);
1378 (*env)->DeleteLocalRef(env, str);
1384 * Loads a class, convert the '.' to '/'.
1387 LoadClass(JNIEnv *env, char *name)
1389 char *buf = JLI_MemAlloc(strlen(name) + 1);
1390 char *s = buf, *t = name, c;
1394 if (_launcher_debug)
1395 start = CounterGet();
1399 *s++ = (c == '.') ? '/' : c;
1400 } while (c != '\0');
1401 cls = (*env)->FindClass(env, buf);
1404 if (_launcher_debug) {
1406 printf("%ld micro seconds to load main class\n",
1407 (long)(jint)Counter2Micros(end-start));
1408 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1416 * Returns the main class name for the specified jar file.
1419 GetMainClassName(JNIEnv *env, char *jarname)
1421 #define MAIN_CLASS "Main-Class"
1424 jobject jar, man, attr;
1425 jstring str, result = 0;
1427 NULL_CHECK0(cls = (*env)->FindClass(env, "java/util/jar/JarFile"));
1428 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1429 "(Ljava/lang/String;)V"));
1430 NULL_CHECK0(str = NewPlatformString(env, jarname));
1431 NULL_CHECK0(jar = (*env)->NewObject(env, cls, mid, str));
1432 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "getManifest",
1433 "()Ljava/util/jar/Manifest;"));
1434 man = (*env)->CallObjectMethod(env, jar, mid);
1436 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1437 (*env)->GetObjectClass(env, man),
1438 "getMainAttributes",
1439 "()Ljava/util/jar/Attributes;"));
1440 attr = (*env)->CallObjectMethod(env, man, mid);
1442 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1443 (*env)->GetObjectClass(env, attr),
1445 "(Ljava/lang/String;)Ljava/lang/String;"));
1446 NULL_CHECK0(str = NewPlatformString(env, MAIN_CLASS));
1447 result = (*env)->CallObjectMethod(env, attr, mid, str);
1454 static char *java_args[] = JAVA_ARGS;
1455 static char *app_classpath[] = APP_CLASSPATH;
1458 * For tools, convert command line args thus:
1459 * javac -cp foo:foo/"*" -J-ms32m ...
1460 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1463 TranslateApplicationArgs(int *pargc, char ***pargv)
1465 const int NUM_ARGS = (sizeof(java_args) / sizeof(char *));
1467 char **argv = *pargv;
1468 int nargc = argc + NUM_ARGS;
1469 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1475 /* Copy the VM arguments (i.e. prefixed with -J) */
1476 for (i = 0; i < NUM_ARGS; i++) {
1477 char *arg = java_args[i];
1478 if (arg[0] == '-' && arg[1] == 'J') {
1483 for (i = 0; i < argc; i++) {
1484 char *arg = argv[i];
1485 if (arg[0] == '-' && arg[1] == 'J') {
1486 if (arg[2] == '\0') {
1487 ReportErrorMessage("Error: the -J option should not be "
1488 "followed by a space.", JNI_TRUE);
1495 /* Copy the rest of the arguments */
1496 for (i = 0; i < NUM_ARGS; i++) {
1497 char *arg = java_args[i];
1498 if (arg[0] != '-' || arg[1] != 'J') {
1502 for (i = 0; i < argc; i++) {
1503 char *arg = argv[i];
1504 if (arg[0] == '-') {
1507 #ifdef EXPAND_CLASSPATH_WILDCARDS
1509 && (strcmp(arg, "-cp") == 0 ||
1510 strcmp(arg, "-classpath") == 0)
1513 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1525 * For our tools, we try to add 3 VM options:
1526 * -Denv.class.path=<envcp>
1527 * -Dapplication.home=<apphome>
1528 * -Djava.class.path=<appcp>
1529 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1530 * tells javac where to find binary classes through this environment
1531 * variable. Notice that users will be able to compile against our
1532 * tools classes (sun.tools.javac.Main) only if they explicitly add
1533 * tools.jar to CLASSPATH.
1534 * <apphome> is the directory where the application is installed.
1535 * <appcp> is the classpath to where our apps' classfiles are.
1538 AddApplicationOptions()
1540 const int NUM_APP_CLASSPATH = (sizeof(app_classpath) / sizeof(char *));
1541 char *envcp, *appcp, *apphome;
1542 char home[MAXPATHLEN]; /* application home */
1543 char separator[] = { PATH_SEPARATOR, '\0' };
1548 const char *s = getenv("CLASSPATH");
1550 s = (char *) JLI_WildcardExpandClasspath(s);
1551 /* 40 for -Denv.class.path= */
1552 envcp = (char *)JLI_MemAlloc(strlen(s) + 40);
1553 sprintf(envcp, "-Denv.class.path=%s", s);
1554 AddOption(envcp, NULL);
1558 if (!GetApplicationHome(home, sizeof(home))) {
1559 ReportErrorMessage("Can't determine application home", JNI_TRUE);
1563 /* 40 for '-Dapplication.home=' */
1564 apphome = (char *)JLI_MemAlloc(strlen(home) + 40);
1565 sprintf(apphome, "-Dapplication.home=%s", home);
1566 AddOption(apphome, NULL);
1568 /* How big is the application's classpath? */
1569 size = 40; /* 40: "-Djava.class.path=" */
1570 strlenHome = (int)strlen(home);
1571 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
1572 size += strlenHome + (int)strlen(app_classpath[i]) + 1; /* 1: separator */
1574 appcp = (char *)JLI_MemAlloc(size + 1);
1575 strcpy(appcp, "-Djava.class.path=");
1576 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
1577 strcat(appcp, home); /* c:\program files\myapp */
1578 strcat(appcp, app_classpath[i]); /* \lib\myapp.jar */
1579 strcat(appcp, separator); /* ; */
1581 appcp[strlen(appcp)-1] = '\0'; /* remove trailing path separator */
1582 AddOption(appcp, NULL);
1585 #endif /* JAVA_ARGS */
1588 * inject the -Dsun.java.command pseudo property into the args structure
1589 * this pseudo property is used in the HotSpot VM to expose the
1590 * Java class name and arguments to the main method to the VM. The
1591 * HotSpot VM uses this pseudo property to store the Java class name
1592 * (or jar file name) and the arguments to the class's main method
1593 * to the instrumentation memory region. The sun.java.command pseudo
1594 * property is not exported by HotSpot to the Java layer.
1597 SetJavaCommandLineProp(char *classname, char *jarfile,
1598 int argc, char **argv)
1603 char* javaCommand = NULL;
1604 char* dashDstr = "-Dsun.java.command=";
1606 if (classname == NULL && jarfile == NULL) {
1607 /* unexpected, one of these should be set. just return without
1608 * setting the property
1613 /* if the class name is not set, then use the jarfile name */
1614 if (classname == NULL) {
1615 classname = jarfile;
1618 /* determine the amount of memory to allocate assuming
1619 * the individual components will be space separated
1621 len = strlen(classname);
1622 for (i = 0; i < argc; i++) {
1623 len += strlen(argv[i]) + 1;
1626 /* allocate the memory */
1627 javaCommand = (char*) JLI_MemAlloc(len + strlen(dashDstr) + 1);
1629 /* build the -D string */
1630 *javaCommand = '\0';
1631 strcat(javaCommand, dashDstr);
1632 strcat(javaCommand, classname);
1634 for (i = 0; i < argc; i++) {
1635 /* the components of the string are space separated. In
1636 * the case of embedded white space, the relationship of
1637 * the white space separated components to their true
1638 * positional arguments will be ambiguous. This issue may
1639 * be addressed in a future release.
1641 strcat(javaCommand, " ");
1642 strcat(javaCommand, argv[i]);
1645 AddOption(javaCommand, NULL);
1649 * JVM would like to know if it's created by a standard Sun launcher, or by
1650 * user native application, the following property indicates the former.
1652 void SetJavaLauncherProp() {
1653 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1657 * Prints the version information from the java.version and other properties.
1660 PrintJavaVersion(JNIEnv *env)
1665 NULL_CHECK(ver = (*env)->FindClass(env, "sun/misc/Version"));
1666 NULL_CHECK(print = (*env)->GetStaticMethodID(env, ver, "print", "()V"));
1668 (*env)->CallStaticVoidMethod(env, ver, print);
1672 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelp.java
1675 PrintUsage(JNIEnv* env, jboolean doXUsage)
1678 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1679 jstring jprogname, vm1, vm2;
1682 NULL_CHECK(cls = (*env)->FindClass(env, "sun/launcher/LauncherHelp"));
1686 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1687 "printXUsageMessage", "(Z)V"));
1688 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1690 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1691 "initHelpMessage", "(Ljava/lang/String;)V"));
1693 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1694 "(Ljava/lang/String;Ljava/lang/String;)V"));
1696 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1697 "appendVmSynonymMessage",
1698 "(Ljava/lang/String;Ljava/lang/String;)V"));
1699 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1700 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1702 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1703 "printHelpMessage", "(Z)V"));
1705 jprogname = (*env)->NewStringUTF(env, progname);
1707 /* Initialize the usage message with the usual preamble */
1708 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1711 /* Assemble the other variant part of the usage */
1712 if ((knownVMs[0].flag == VM_KNOWN) ||
1713 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1714 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1715 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1716 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1718 for (i=1; i<knownVMsCount; i++) {
1719 if (knownVMs[i].flag == VM_KNOWN) {
1720 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1721 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1722 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1725 for (i=1; i<knownVMsCount; i++) {
1726 if (knownVMs[i].flag == VM_ALIASED_TO) {
1727 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1728 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1729 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1733 /* The first known VM is the default */
1735 jboolean isServerClassMachine = ServerClassMachine();
1737 const char* defaultVM = knownVMs[0].name+1;
1738 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1739 defaultVM = knownVMs[0].server_class+1;
1742 vm1 = (*env)->NewStringUTF(env, defaultVM);
1743 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1746 /* Complete the usage message and print to stderr*/
1747 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1753 * Read the jvm.cfg file and fill the knownJVMs[] array.
1755 * The functionality of the jvm.cfg file is subject to change without
1756 * notice and the mechanism will be removed in the future.
1758 * The lexical structure of the jvm.cfg file is as follows:
1760 * jvmcfg := { vmLine }
1761 * vmLine := knownLine
1768 * knownLine := flag "KNOWN" EOL
1769 * warnLine := flag "WARN" EOL
1770 * ignoreLine := flag "IGNORE" EOL
1771 * errorLine := flag "ERROR" EOL
1772 * aliasLine := flag "ALIASED_TO" flag EOL
1773 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1774 * commentLine := "#" text EOL
1775 * flag := "-" identifier
1777 * The semantics are that when someone specifies a flag on the command line:
1778 * - if the flag appears on a knownLine, then the identifier is used as
1779 * the name of the directory holding the JVM library (the name of the JVM).
1780 * - if the flag appears as the first flag on an aliasLine, the identifier
1781 * of the second flag is used as the name of the JVM.
1782 * - if the flag appears on a warnLine, the identifier is used as the
1783 * name of the JVM, but a warning is generated.
1784 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1785 * name of a JVM, but the identifier is ignored and the default vm used
1786 * - if the flag appears on an errorLine, an error is generated.
1787 * - if the flag appears as the first flag on a predicateLine, and
1788 * the machine on which you are running passes the predicate indicated,
1789 * then the identifier of the second flag is used as the name of the JVM,
1790 * otherwise the identifier of the first flag is used as the name of the JVM.
1791 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1792 * file determines the name of the JVM.
1793 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1794 * since they only make sense if someone hasn't specified the name of the
1795 * JVM on the command line.
1797 * The intent of the jvm.cfg file is to allow several JVM libraries to
1798 * be installed in different subdirectories of a single JRE installation,
1799 * for space-savings and convenience in testing.
1800 * The intent is explicitly not to provide a full aliasing or predicate
1804 ReadKnownVMs(const char *jrepath, char * arch, jboolean speculative)
1807 char jvmCfgName[MAXPATHLEN+20];
1808 char line[MAXPATHLEN+20];
1814 char *altVMName = NULL;
1815 char *serverClassVMName = NULL;
1816 static char *whiteSpace = " \t";
1817 if (_launcher_debug) {
1818 start = CounterGet();
1821 strcpy(jvmCfgName, jrepath);
1822 strcat(jvmCfgName, FILESEP "lib" FILESEP);
1823 strcat(jvmCfgName, arch);
1824 strcat(jvmCfgName, FILESEP "jvm.cfg");
1826 jvmCfg = fopen(jvmCfgName, "r");
1827 if (jvmCfg == NULL) {
1829 ReportErrorMessage2("Error: could not open `%s'", jvmCfgName,
1836 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1837 vmType = VM_UNKNOWN;
1841 if (line[0] != '-') {
1842 fprintf(stderr, "Warning: no leading - on line %d of `%s'\n",
1843 lineno, jvmCfgName);
1845 if (cnt >= knownVMsLimit) {
1848 line[strlen(line)-1] = '\0'; /* remove trailing newline */
1849 tmpPtr = line + strcspn(line, whiteSpace);
1851 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
1852 lineno, jvmCfgName);
1854 /* Null-terminate this string for JLI_StringDup below */
1856 tmpPtr += strspn(tmpPtr, whiteSpace);
1858 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
1859 lineno, jvmCfgName);
1861 if (!strncmp(tmpPtr, "KNOWN", strlen("KNOWN"))) {
1863 } else if (!strncmp(tmpPtr, "ALIASED_TO", strlen("ALIASED_TO"))) {
1864 tmpPtr += strcspn(tmpPtr, whiteSpace);
1866 tmpPtr += strspn(tmpPtr, whiteSpace);
1869 fprintf(stderr, "Warning: missing VM alias on line %d of `%s'\n",
1870 lineno, jvmCfgName);
1872 /* Null terminate altVMName */
1874 tmpPtr += strcspn(tmpPtr, whiteSpace);
1876 vmType = VM_ALIASED_TO;
1878 } else if (!strncmp(tmpPtr, "WARN", strlen("WARN"))) {
1880 } else if (!strncmp(tmpPtr, "IGNORE", strlen("IGNORE"))) {
1882 } else if (!strncmp(tmpPtr, "ERROR", strlen("ERROR"))) {
1884 } else if (!strncmp(tmpPtr,
1886 strlen("IF_SERVER_CLASS"))) {
1887 tmpPtr += strcspn(tmpPtr, whiteSpace);
1889 tmpPtr += strspn(tmpPtr, whiteSpace);
1892 fprintf(stderr, "Warning: missing server class VM on line %d of `%s'\n",
1893 lineno, jvmCfgName);
1895 /* Null terminate server class VM name */
1896 serverClassVMName = tmpPtr;
1897 tmpPtr += strcspn(tmpPtr, whiteSpace);
1899 vmType = VM_IF_SERVER_CLASS;
1902 fprintf(stderr, "Warning: unknown VM type on line %d of `%s'\n",
1903 lineno, &jvmCfgName[0]);
1909 if (_launcher_debug)
1910 printf("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1911 if (vmType != VM_UNKNOWN) {
1912 knownVMs[cnt].name = JLI_StringDup(line);
1913 knownVMs[cnt].flag = vmType;
1918 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1919 if (_launcher_debug) {
1920 printf(" name: %s vmType: %s alias: %s\n",
1921 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1924 case VM_IF_SERVER_CLASS:
1925 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1926 if (_launcher_debug) {
1927 printf(" name: %s vmType: %s server_class: %s\n",
1928 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1936 knownVMsCount = cnt;
1938 if (_launcher_debug) {
1940 printf("%ld micro seconds to parse jvm.cfg\n",
1941 (long)(jint)Counter2Micros(end-start));
1949 GrowKnownVMs(int minimum)
1951 struct vmdesc* newKnownVMs;
1954 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1955 if (newMax <= minimum) {
1958 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1959 if (knownVMs != NULL) {
1960 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1962 JLI_MemFree(knownVMs);
1963 knownVMs = newKnownVMs;
1964 knownVMsLimit = newMax;
1968 /* Returns index of VM or -1 if not found */
1970 KnownVMIndex(const char* name)
1973 if (strncmp(name, "-J", 2) == 0) name += 2;
1974 for (i = 0; i < knownVMsCount; i++) {
1975 if (!strcmp(name, knownVMs[i].name)) {
1986 for (i = 0; i < knownVMsCount; i++) {
1987 JLI_MemFree(knownVMs[i].name);
1988 knownVMs[i].name = NULL;
1990 JLI_MemFree(knownVMs);
1995 * Displays the splash screen according to the jar file name
1996 * and image file names stored in environment variables
2001 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
2002 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
2006 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
2009 DoSplashLoadMemory(image_data, data_size);
2010 JLI_MemFree(image_data);
2012 } else if (file_name) {
2014 DoSplashLoadFile(file_name);
2018 DoSplashSetFileJarName(file_name, jar_name);