source: trunk/openjdk/jdk/src/share/bin/java.c

Last change on this file was 407, checked in by dmik, 13 years ago

jdk: Use stdout instead of stderr for printing usage info from the JAVA launcher.

Printing usage info to stderr is not respected on OS/2. See also #64 for more details.

File size: 67.2 KB
Line 
1/*
2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * Shared source for 'java' command line tool.
28 *
29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
30 * instance, the JDK command line tools such as javac and javadoc (see
31 * makefiles for more details) are built with this program. Any arguments
32 * prefixed with '-J' will be passed directly to the 'java' command.
33 */
34
35/*
36 * One job of the launcher is to remove command line options which the
37 * vm does not understand and will not process. These options include
38 * options which select which style of vm is run (e.g. -client and
39 * -server) as well as options which select the data model to use.
40 * Additionally, for tools which invoke an underlying vm "-J-foo"
41 * options are turned into "-foo" options to the vm. This option
42 * filtering is handled in a number of places in the launcher, some of
43 * it in machine-dependent code. In this file, the function
44 * CheckJVMType removes vm style options and TranslateApplicationArgs
45 * removes "-J" prefixes. 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.
54 */
55
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59
60#ifdef __OS2__
61#include <float.h> // for _control87
62#endif
63
64#include <jni.h>
65#include <jvm.h>
66#include "java.h"
67#include "manifest_info.h"
68#include "version_comp.h"
69#include "wildcard.h"
70#include "splashscreen.h"
71
72#ifndef FULL_VERSION
73#define FULL_VERSION JDK_MAJOR_VERSION "." JDK_MINOR_VERSION
74#endif
75
76/*
77 * The following environment variable is used to influence the behavior
78 * of the jre exec'd through the SelectVersion routine. The command line
79 * options which specify the version are not passed to the exec'd version,
80 * because that jre may be an older version which wouldn't recognize them.
81 * This environment variable is known to this (and later) version and serves
82 * to suppress the version selection code. This is not only for efficiency,
83 * but also for correctness, since any command line options have been
84 * removed which would cause any value found in the manifest to be used.
85 * This would be incorrect because the command line options are defined
86 * to take precedence.
87 *
88 * The value associated with this environment variable is the MainClass
89 * name from within the executable jar file (if any). This is strictly a
90 * performance enhancement to avoid re-reading the jar file manifest.
91 *
92 * A NOTE TO DEVELOPERS: For performance reasons it is important that
93 * the program image remain relatively small until after SelectVersion
94 * CreateExecutionEnvironment have finished their possibly recursive
95 * processing. Watch everything, but resist all temptations to use Java
96 * interfaces.
97 */
98#define ENV_ENTRY "_JAVA_VERSION_SET"
99
100#define SPLASH_FILE_ENV_ENTRY "_JAVA_SPLASH_FILE"
101#define SPLASH_JAR_ENV_ENTRY "_JAVA_SPLASH_JAR"
102
103static jboolean printVersion = JNI_FALSE; /* print and exit */
104static jboolean showVersion = JNI_FALSE; /* print but continue */
105static jboolean printUsage = JNI_FALSE; /* print and exit*/
106static jboolean printXUsage = JNI_FALSE; /* print and exit*/
107static char *progname;
108static char *launchername;
109jboolean _launcher_debug = JNI_FALSE;
110
111/*
112 * Entries for splash screen environment variables.
113 * putenv is performed in SelectVersion. We need
114 * them in memory until UnsetEnv, so they are made static
115 * global instead of auto local.
116 */
117static char* splash_file_entry = NULL;
118static char* splash_jar_entry = NULL;
119
120/*
121 * List of VM options to be specified when the VM is created.
122 */
123static JavaVMOption *options;
124static int numOptions, maxOptions;
125
126/*
127 * Prototypes for functions internal to launcher.
128 */
129static void SetClassPath(const char *s);
130static void SelectVersion(int argc, char **argv, char **main_class);
131static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
132 char **pclassname, int *pret, const char *jvmpath);
133static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
134 InvocationFunctions *ifn);
135static jstring NewPlatformString(JNIEnv *env, char *s);
136static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
137static jclass LoadClass(JNIEnv *env, char *name);
138static jstring GetMainClassName(JNIEnv *env, char *jarname);
139static void SetJavaCommandLineProp(char* classname, char* jarfile, int argc, char** argv);
140static void SetJavaLauncherProp(void);
141
142#ifdef JAVA_ARGS
143static void TranslateApplicationArgs(int *pargc, char ***pargv);
144static jboolean AddApplicationOptions(void);
145#endif
146
147static void PrintJavaVersion(JNIEnv *env);
148static void PrintUsage(JNIEnv* env, jboolean doXUsage);
149
150static void SetPaths(int argc, char **argv);
151
152
153/* Maximum supported entries from jvm.cfg. */
154#define INIT_MAX_KNOWN_VMS 10
155/* Values for vmdesc.flag */
156#define VM_UNKNOWN -1
157#define VM_KNOWN 0
158#define VM_ALIASED_TO 1
159#define VM_WARN 2
160#define VM_ERROR 3
161#define VM_IF_SERVER_CLASS 4
162#define VM_IGNORE 5
163struct vmdesc {
164 char *name;
165 int flag;
166 char *alias;
167 char *server_class;
168};
169static struct vmdesc *knownVMs = NULL;
170static int knownVMsCount = 0;
171static int knownVMsLimit = 0;
172
173static void GrowKnownVMs();
174static int KnownVMIndex(const char* name);
175static void FreeKnownVMs();
176static void ShowSplashScreen();
177
178jboolean ServerClassMachine();
179
180/* flag which if set suppresses error messages from the launcher */
181static int noExitErrorMessage = 0;
182
183/*
184 * Running Java code in primordial thread caused many problems. We will
185 * create a new thread to invoke JVM. See 6316197 for more information.
186 */
187static jlong threadStackSize = 0; /* stack size of the new thread */
188
189int JNICALL JavaMain(void * args); /* entry point */
190
191struct JavaMainArgs {
192 int argc;
193 char ** argv;
194 char * jarfile;
195 char * classname;
196 InvocationFunctions ifn;
197};
198
199/*
200 * Entry point.
201 */
202int
203main(int argc, char ** argv)
204{
205 char *jarfile = 0;
206 char *classname = 0;
207 char *s = 0;
208 char *main_class = NULL;
209 int ret;
210 InvocationFunctions ifn;
211 jlong start = 0, end;
212 char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];
213 char ** original_argv = argv;
214
215 if (getenv("_JAVA_LAUNCHER_DEBUG") != 0) {
216 int i;
217 _launcher_debug = JNI_TRUE;
218 printf("----_JAVA_LAUNCHER_DEBUG----\n");
219 printf("Command line Args:\n");
220 for(i = 0; i < argc+1; i++) {
221 if (argv[i] != NULL){
222 printf("\targv[%d] = '%s'\n",i,argv[i]);
223 }
224 }
225 }
226
227
228
229 /*
230 * Make sure the specified version of the JRE is running.
231 *
232 * There are three things to note about the SelectVersion() routine:
233 * 1) If the version running isn't correct, this routine doesn't
234 * return (either the correct version has been exec'd or an error
235 * was issued).
236 * 2) Argc and Argv in this scope are *not* altered by this routine.
237 * It is the responsibility of subsequent code to ignore the
238 * arguments handled by this routine.
239 * 3) As a side-effect, the variable "main_class" is guaranteed to
240 * be set (if it should ever be set). This isn't exactly the
241 * poster child for structured programming, but it is a small
242 * price to pay for not processing a jar file operand twice.
243 * (Note: This side effect has been disabled. See comment on
244 * bugid 5030265 below.)
245 */
246 SelectVersion(argc, argv, &main_class);
247
248 /* copy original argv */
249 {
250 int i;
251 original_argv = (char**)JLI_MemAlloc(sizeof(char*)*(argc+1));
252 for(i = 0; i < argc+1; i++) {
253 original_argv[i] = argv[i];
254 }
255 }
256
257 CreateExecutionEnvironment(&argc, &argv,
258 jrepath, sizeof(jrepath),
259 jvmpath, sizeof(jvmpath),
260 original_argv);
261
262 ifn.CreateJavaVM = 0;
263 ifn.GetDefaultJavaVMInitArgs = 0;
264
265 if (_launcher_debug)
266 start = CounterGet();
267 if (!LoadJavaVM(jvmpath, &ifn)) {
268 exit(6);
269 }
270 if (_launcher_debug) {
271 end = CounterGet();
272 printf("%ld micro seconds to LoadJavaVM\n",
273 (long)(jint)Counter2Micros(end-start));
274 }
275
276#ifdef JAVA_ARGS /* javac, jar and friends. */
277 progname = "java";
278#else /* java, oldjava, javaw and friends */
279#ifdef PROGNAME
280 progname = PROGNAME;
281#else
282 progname = *argv;
283 if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
284 progname = s + 1;
285 }
286#endif /* PROGNAME */
287#endif /* JAVA_ARGS */
288
289#ifdef LAUNCHER_NAME
290 launchername = LAUNCHER_NAME;
291#else
292 launchername = progname;
293#endif /* LAUNCHER_NAME */
294
295 ++argv;
296 --argc;
297
298#ifdef JAVA_ARGS
299 /* Preprocess wrapper arguments */
300 TranslateApplicationArgs(&argc, &argv);
301 if (!AddApplicationOptions()) {
302 exit(1);
303 }
304#endif
305
306 /* Set default CLASSPATH */
307 if ((s = getenv("CLASSPATH")) == 0) {
308 s = ".";
309 }
310#ifndef JAVA_ARGS
311 SetClassPath(s);
312#endif
313
314 /*
315 * Parse command line options; if the return value of
316 * ParseArguments is false, the program should exit.
317 */
318 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
319 exit(ret);
320 }
321
322 /* Override class path if -jar flag was specified */
323 if (jarfile != 0) {
324 SetClassPath(jarfile);
325 }
326
327 /* set the -Dsun.java.command pseudo property */
328 SetJavaCommandLineProp(classname, jarfile, argc, argv);
329
330 /* Set the -Dsun.java.launcher pseudo property */
331 SetJavaLauncherProp();
332
333 /* set the -Dsun.java.launcher.* platform properties */
334 SetJavaLauncherPlatformProps();
335
336 /* Show the splash screen if needed */
337 ShowSplashScreen();
338
339 /*
340 * Done with all command line processing and potential re-execs so
341 * clean up the environment.
342 */
343 (void)UnsetEnv(ENV_ENTRY);
344 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
345 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
346
347 JLI_MemFree(splash_jar_entry);
348 JLI_MemFree(splash_file_entry);
349
350 /*
351 * If user doesn't specify stack size, check if VM has a preference.
352 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
353 * return its default stack size through the init args structure.
354 */
355 if (threadStackSize == 0) {
356 struct JDK1_1InitArgs args1_1;
357 memset((void*)&args1_1, 0, sizeof(args1_1));
358 args1_1.version = JNI_VERSION_1_1;
359 ifn.GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
360 if (args1_1.javaStackSize > 0) {
361 threadStackSize = args1_1.javaStackSize;
362 }
363 }
364
365 { /* Create a new thread to create JVM and invoke main method */
366 struct JavaMainArgs args;
367
368 args.argc = argc;
369 args.argv = argv;
370 args.jarfile = jarfile;
371 args.classname = classname;
372 args.ifn = ifn;
373
374#ifdef __OS2__
375 // On OS/2, both Ctrl-C and process termination handlers (including DLL
376 // uninitialization code) are always called on thread 1. If we fire off
377 // a new thread here and create the JVM on it (as other platfroms do),
378 // thread 1 will be not known to the JVM and this can cause various
379 // nexpected side-effects like hangs and crashes at Java process
380 // termination (see #33 and #159 for details). For this reason, we instead
381 // create the JVM right on thread 1.
382
383 // disable FPU exceptions (taken from jdk/src/windows/hpi/src/system_md.c)
384 _control87(MCW_EM | RC_NEAR | PC_53, MCW_EM | MCW_RC | MCW_PC);
385
386 return JavaMain(&args);
387#else
388 return ContinueInNewThread(JavaMain, threadStackSize, (void*)&args, ret);
389#endif
390 }
391}
392
393int JNICALL
394JavaMain(void * _args)
395{
396 struct JavaMainArgs *args = (struct JavaMainArgs *)_args;
397 int argc = args->argc;
398 char **argv = args->argv;
399 char *jarfile = args->jarfile;
400 char *classname = args->classname;
401 InvocationFunctions ifn = args->ifn;
402
403 JavaVM *vm = 0;
404 JNIEnv *env = 0;
405 jstring mainClassName;
406 jclass mainClass;
407 jmethodID mainID;
408 jobjectArray mainArgs;
409 int ret = 0;
410 jlong start = 0, end;
411
412 /*
413 * Error message to print or display; by default the message will
414 * only be displayed in a window.
415 */
416 char * message = "Fatal exception occurred. Program will exit.";
417 jboolean messageDest = JNI_FALSE;
418
419 /* Initialize the virtual machine */
420
421 if (_launcher_debug)
422 start = CounterGet();
423 if (!InitializeJVM(&vm, &env, &ifn)) {
424 ReportErrorMessage("Could not create the Java virtual machine.",
425 JNI_TRUE);
426 exit(1);
427 }
428
429 if (printVersion || showVersion) {
430 PrintJavaVersion(env);
431 if ((*env)->ExceptionOccurred(env)) {
432 ReportExceptionDescription(env);
433 goto leave;
434 }
435 if (printVersion) {
436 ret = 0;
437 message = NULL;
438 goto leave;
439 }
440 if (showVersion) {
441 fprintf(stderr, "\n");
442 }
443 }
444
445 /* If the user specified neither a class name nor a JAR file */
446 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
447 PrintUsage(env, printXUsage);
448 if ((*env)->ExceptionOccurred(env)) {
449 ReportExceptionDescription(env);
450 ret=1;
451 }
452 message = NULL;
453 goto leave;
454 }
455
456
457
458 FreeKnownVMs(); /* after last possible PrintUsage() */
459
460 if (_launcher_debug) {
461 end = CounterGet();
462 printf("%ld micro seconds to InitializeJVM\n",
463 (long)(jint)Counter2Micros(end-start));
464 }
465
466 /* At this stage, argc/argv have the applications' arguments */
467 if (_launcher_debug) {
468 int i = 0;
469 printf("Main-Class is '%s'\n", classname ? classname : "");
470 printf("Apps' argc is %d\n", argc);
471 for (; i < argc; i++) {
472 printf(" argv[%2d] = '%s'\n", i, argv[i]);
473 }
474 }
475
476 ret = 1;
477
478 /*
479 * Get the application's main class.
480 *
481 * See bugid 5030265. The Main-Class name has already been parsed
482 * from the manifest, but not parsed properly for UTF-8 support.
483 * Hence the code here ignores the value previously extracted and
484 * uses the pre-existing code to reextract the value. This is
485 * possibly an end of release cycle expedient. However, it has
486 * also been discovered that passing some character sets through
487 * the environment has "strange" behavior on some variants of
488 * Windows. Hence, maybe the manifest parsing code local to the
489 * launcher should never be enhanced.
490 *
491 * Hence, future work should either:
492 * 1) Correct the local parsing code and verify that the
493 * Main-Class attribute gets properly passed through
494 * all environments,
495 * 2) Remove the vestages of maintaining main_class through
496 * the environment (and remove these comments).
497 */
498 if (jarfile != 0) {
499 mainClassName = GetMainClassName(env, jarfile);
500 if ((*env)->ExceptionOccurred(env)) {
501 ReportExceptionDescription(env);
502 goto leave;
503 }
504 if (mainClassName == NULL) {
505 const char * format = "Failed to load Main-Class manifest "
506 "attribute from\n%s";
507 message = (char*)JLI_MemAlloc((strlen(format) + strlen(jarfile)) *
508 sizeof(char));
509 sprintf(message, format, jarfile);
510 messageDest = JNI_TRUE;
511 goto leave;
512 }
513 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
514 if (classname == NULL) {
515 ReportExceptionDescription(env);
516 goto leave;
517 }
518 mainClass = LoadClass(env, classname);
519 if(mainClass == NULL) { /* exception occured */
520 const char * format = "Could not find the main class: %s. Program will exit.";
521 ReportExceptionDescription(env);
522 message = (char *)JLI_MemAlloc((strlen(format) +
523 strlen(classname)) * sizeof(char));
524 messageDest = JNI_TRUE;
525 sprintf(message, format, classname);
526 goto leave;
527 }
528 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
529 } else {
530 mainClassName = NewPlatformString(env, classname);
531 if (mainClassName == NULL) {
532 const char * format = "Failed to load Main Class: %s";
533 message = (char *)JLI_MemAlloc((strlen(format) + strlen(classname)) *
534 sizeof(char) );
535 sprintf(message, format, classname);
536 messageDest = JNI_TRUE;
537 goto leave;
538 }
539 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
540 if (classname == NULL) {
541 ReportExceptionDescription(env);
542 goto leave;
543 }
544 mainClass = LoadClass(env, classname);
545 if(mainClass == NULL) { /* exception occured */
546 const char * format = "Could not find the main class: %s. Program will exit.";
547 ReportExceptionDescription(env);
548 message = (char *)JLI_MemAlloc((strlen(format) +
549 strlen(classname)) * sizeof(char));
550 messageDest = JNI_TRUE;
551 sprintf(message, format, classname);
552 goto leave;
553 }
554 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
555 }
556
557 /* Get the application's main method */
558 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
559 "([Ljava/lang/String;)V");
560 if (mainID == NULL) {
561 if ((*env)->ExceptionOccurred(env)) {
562 ReportExceptionDescription(env);
563 } else {
564 message = "No main method found in specified class.";
565 messageDest = JNI_TRUE;
566 }
567 goto leave;
568 }
569
570 { /* Make sure the main method is public */
571 jint mods;
572 jmethodID mid;
573 jobject obj = (*env)->ToReflectedMethod(env, mainClass,
574 mainID, JNI_TRUE);
575
576 if( obj == NULL) { /* exception occurred */
577 ReportExceptionDescription(env);
578 goto leave;
579 }
580
581 mid =
582 (*env)->GetMethodID(env,
583 (*env)->GetObjectClass(env, obj),
584 "getModifiers", "()I");
585 if ((*env)->ExceptionOccurred(env)) {
586 ReportExceptionDescription(env);
587 goto leave;
588 }
589
590 mods = (*env)->CallIntMethod(env, obj, mid);
591 if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
592 message = "Main method not public.";
593 messageDest = JNI_TRUE;
594 goto leave;
595 }
596 }
597
598 /* Build argument array */
599 mainArgs = NewPlatformStringArray(env, argv, argc);
600 if (mainArgs == NULL) {
601 ReportExceptionDescription(env);
602 goto leave;
603 }
604
605 /* Invoke main method. */
606 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
607
608 /*
609 * The launcher's exit code (in the absence of calls to
610 * System.exit) will be non-zero if main threw an exception.
611 */
612 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
613
614 /*
615 * Detach the main thread so that it appears to have ended when
616 * the application's main method exits. This will invoke the
617 * uncaught exception handler machinery if main threw an
618 * exception. An uncaught exception handler cannot change the
619 * launcher's return code except by calling System.exit.
620 */
621 if ((*vm)->DetachCurrentThread(vm) != 0) {
622 message = "Could not detach main thread.";
623 messageDest = JNI_TRUE;
624 ret = 1;
625 goto leave;
626 }
627
628 message = NULL;
629
630 leave:
631 /*
632 * Wait for all non-daemon threads to end, then destroy the VM.
633 * This will actually create a trivial new Java waiter thread
634 * named "DestroyJavaVM", but this will be seen as a different
635 * thread from the one that executed main, even though they are
636 * the same C thread. This allows mainThread.join() and
637 * mainThread.isAlive() to work as expected.
638 */
639 (*vm)->DestroyJavaVM(vm);
640
641 if(message != NULL && !noExitErrorMessage)
642 ReportErrorMessage(message, messageDest);
643 return ret;
644}
645
646
647/*
648 * Checks the command line options to find which JVM type was
649 * specified. If no command line option was given for the JVM type,
650 * the default type is used. The environment variable
651 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
652 * checked as ways of specifying which JVM type to invoke.
653 */
654char *
655CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
656 int i, argi;
657 int argc;
658 char **newArgv;
659 int newArgvIdx = 0;
660 int isVMType;
661 int jvmidx = -1;
662 char *jvmtype = getenv("JDK_ALTERNATE_VM");
663
664 argc = *pargc;
665
666 /* To make things simpler we always copy the argv array */
667 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
668
669 /* The program name is always present */
670 newArgv[newArgvIdx++] = (*argv)[0];
671
672 for (argi = 1; argi < argc; argi++) {
673 char *arg = (*argv)[argi];
674 isVMType = 0;
675
676#ifdef JAVA_ARGS
677 if (arg[0] != '-') {
678 newArgv[newArgvIdx++] = arg;
679 continue;
680 }
681#else
682 if (strcmp(arg, "-classpath") == 0 ||
683 strcmp(arg, "-cp") == 0) {
684 newArgv[newArgvIdx++] = arg;
685 argi++;
686 if (argi < argc) {
687 newArgv[newArgvIdx++] = (*argv)[argi];
688 }
689 continue;
690 }
691 if (arg[0] != '-') break;
692#endif
693
694 /* Did the user pass an explicit VM type? */
695 i = KnownVMIndex(arg);
696 if (i >= 0) {
697 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
698 isVMType = 1;
699 *pargc = *pargc - 1;
700 }
701
702 /* Did the user specify an "alternate" VM? */
703 else if (strncmp(arg, "-XXaltjvm=", 10) == 0 || strncmp(arg, "-J-XXaltjvm=", 12) == 0) {
704 isVMType = 1;
705 jvmtype = arg+((arg[1]=='X')? 10 : 12);
706 jvmidx = -1;
707 }
708
709 if (!isVMType) {
710 newArgv[newArgvIdx++] = arg;
711 }
712 }
713
714 /*
715 * Finish copying the arguments if we aborted the above loop.
716 * NOTE that if we aborted via "break" then we did NOT copy the
717 * last argument above, and in addition argi will be less than
718 * argc.
719 */
720 while (argi < argc) {
721 newArgv[newArgvIdx++] = (*argv)[argi];
722 argi++;
723 }
724
725 /* argv is null-terminated */
726 newArgv[newArgvIdx] = 0;
727
728 /* Copy back argv */
729 *argv = newArgv;
730 *pargc = newArgvIdx;
731
732 /* use the default VM type if not specified (no alias processing) */
733 if (jvmtype == NULL) {
734 char* result = knownVMs[0].name+1;
735 /* Use a different VM type if we are on a server class machine? */
736 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
737 (ServerClassMachine() == JNI_TRUE)) {
738 result = knownVMs[0].server_class+1;
739 }
740 if (_launcher_debug) {
741 printf("Default VM: %s\n", result);
742 }
743 return result;
744 }
745
746 /* if using an alternate VM, no alias processing */
747 if (jvmidx < 0)
748 return jvmtype;
749
750 /* Resolve aliases first */
751 {
752 int loopCount = 0;
753 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
754 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
755
756 if (loopCount > knownVMsCount) {
757 if (!speculative) {
758 ReportErrorMessage("Error: Corrupt jvm.cfg file; cycle in alias list.",
759 JNI_TRUE);
760 exit(1);
761 } else {
762 return "ERROR";
763 /* break; */
764 }
765 }
766
767 if (nextIdx < 0) {
768 if (!speculative) {
769 ReportErrorMessage2("Error: Unable to resolve VM alias %s",
770 knownVMs[jvmidx].alias, JNI_TRUE);
771 exit(1);
772 } else {
773 return "ERROR";
774 }
775 }
776 jvmidx = nextIdx;
777 jvmtype = knownVMs[jvmidx].name+1;
778 loopCount++;
779 }
780 }
781
782 switch (knownVMs[jvmidx].flag) {
783 case VM_WARN:
784 if (!speculative) {
785 fprintf(stderr, "Warning: %s VM not supported; %s VM will be used\n",
786 jvmtype, knownVMs[0].name + 1);
787 }
788 /* fall through */
789 case VM_IGNORE:
790 jvmtype = knownVMs[jvmidx=0].name + 1;
791 /* fall through */
792 case VM_KNOWN:
793 break;
794 case VM_ERROR:
795 if (!speculative) {
796 ReportErrorMessage2("Error: %s VM not supported", jvmtype, JNI_TRUE);
797 exit(1);
798 } else {
799 return "ERROR";
800 }
801 }
802
803 return jvmtype;
804}
805
806# define KB (1024UL)
807# define MB (1024UL * KB)
808# define GB (1024UL * MB)
809
810/* copied from HotSpot function "atomll()" */
811static int
812parse_stack_size(const char *s, jlong *result) {
813 jlong n = 0;
814 int args_read = sscanf(s, jlong_format_specifier(), &n);
815 if (args_read != 1) {
816 return 0;
817 }
818 while (*s != '\0' && *s >= '0' && *s <= '9') {
819 s++;
820 }
821 // 4705540: illegal if more characters are found after the first non-digit
822 if (strlen(s) > 1) {
823 return 0;
824 }
825 switch (*s) {
826 case 'T': case 't':
827 *result = n * GB * KB;
828 return 1;
829 case 'G': case 'g':
830 *result = n * GB;
831 return 1;
832 case 'M': case 'm':
833 *result = n * MB;
834 return 1;
835 case 'K': case 'k':
836 *result = n * KB;
837 return 1;
838 case '\0':
839 *result = n;
840 return 1;
841 default:
842 /* Create JVM with default stack and let VM handle malformed -Xss string*/
843 return 0;
844 }
845}
846
847/*
848 * Adds a new VM option with the given given name and value.
849 */
850void
851AddOption(char *str, void *info)
852{
853 /*
854 * Expand options array if needed to accommodate at least one more
855 * VM option.
856 */
857 if (numOptions >= maxOptions) {
858 if (options == 0) {
859 maxOptions = 4;
860 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
861 } else {
862 JavaVMOption *tmp;
863 maxOptions *= 2;
864 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
865 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
866 JLI_MemFree(options);
867 options = tmp;
868 }
869 }
870 options[numOptions].optionString = str;
871 options[numOptions++].extraInfo = info;
872
873 if (strncmp(str, "-Xss", 4) == 0) {
874 jlong tmp;
875 if (parse_stack_size(str + 4, &tmp)) {
876 threadStackSize = tmp;
877 }
878 }
879}
880
881static void
882SetClassPath(const char *s)
883{
884 char *def;
885 s = JLI_WildcardExpandClasspath(s);
886 def = JLI_MemAlloc(strlen(s) + 40);
887 sprintf(def, "-Djava.class.path=%s", s);
888 AddOption(def, NULL);
889}
890
891/*
892 * The SelectVersion() routine ensures that an appropriate version of
893 * the JRE is running. The specification for the appropriate version
894 * is obtained from either the manifest of a jar file (preferred) or
895 * from command line options.
896 * The routine also parses splash screen command line options and
897 * passes on their values in private environment variables.
898 */
899static void
900SelectVersion(int argc, char **argv, char **main_class)
901{
902 char *arg;
903 char **new_argv;
904 char **new_argp;
905 char *operand;
906 char *version = NULL;
907 char *jre = NULL;
908 int jarflag = 0;
909 int headlessflag = 0;
910 int restrict_search = -1; /* -1 implies not known */
911 manifest_info info;
912 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
913 char *splash_file_name = NULL;
914 char *splash_jar_name = NULL;
915 char *env_in;
916 int res;
917
918 /*
919 * If the version has already been selected, set *main_class
920 * with the value passed through the environment (if any) and
921 * simply return.
922 */
923 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
924 if (*env_in != '\0')
925 *main_class = JLI_StringDup(env_in);
926 return;
927 }
928
929 /*
930 * Scan through the arguments for options relevant to multiple JRE
931 * support. For reference, the command line syntax is defined as:
932 *
933 * SYNOPSIS
934 * java [options] class [argument...]
935 *
936 * java [options] -jar file.jar [argument...]
937 *
938 * As the scan is performed, make a copy of the argument list with
939 * the version specification options (new to 1.5) removed, so that
940 * a version less than 1.5 can be exec'd.
941 *
942 * Note that due to the syntax of the native Windows interface
943 * CreateProcess(), processing similar to the following exists in
944 * the Windows platform specific routine ExecJRE (in java_md.c).
945 * Changes here should be reproduced there.
946 */
947 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
948 new_argv[0] = argv[0];
949 new_argp = &new_argv[1];
950 argc--;
951 argv++;
952 while ((arg = *argv) != 0 && *arg == '-') {
953 if (strncmp(arg, "-version:", 9) == 0) {
954 version = arg + 9;
955 } else if (strcmp(arg, "-jre-restrict-search") == 0) {
956 restrict_search = 1;
957 } else if (strcmp(arg, "-no-jre-restrict-search") == 0) {
958 restrict_search = 0;
959 } else {
960 if (strcmp(arg, "-jar") == 0)
961 jarflag = 1;
962 /* deal with "unfortunate" classpath syntax */
963 if ((strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) &&
964 (argc >= 2)) {
965 *new_argp++ = arg;
966 argc--;
967 argv++;
968 arg = *argv;
969 }
970
971 /*
972 * Checking for headless toolkit option in the some way as AWT does:
973 * "true" means true and any other value means false
974 */
975 if (strcmp(arg, "-Djava.awt.headless=true") == 0) {
976 headlessflag = 1;
977 } else if (strncmp(arg, "-Djava.awt.headless=", 20) == 0) {
978 headlessflag = 0;
979 } else if (strncmp(arg, "-splash:", 8) == 0) {
980 splash_file_name = arg+8;
981 }
982 *new_argp++ = arg;
983 }
984 argc--;
985 argv++;
986 }
987 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
988 operand = NULL;
989 } else {
990 argc--;
991 *new_argp++ = operand = *argv++;
992 }
993 while (argc-- > 0) /* Copy over [argument...] */
994 *new_argp++ = *argv++;
995 *new_argp = NULL;
996
997 /*
998 * If there is a jar file, read the manifest. If the jarfile can't be
999 * read, the manifest can't be read from the jar file, or the manifest
1000 * is corrupt, issue the appropriate error messages and exit.
1001 *
1002 * Even if there isn't a jar file, construct a manifest_info structure
1003 * containing the command line information. It's a convenient way to carry
1004 * this data around.
1005 */
1006 if (jarflag && operand) {
1007 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
1008 if (res == -1)
1009 ReportErrorMessage2("Unable to access jarfile %s",
1010 operand, JNI_TRUE);
1011 else
1012 ReportErrorMessage2("Invalid or corrupt jarfile %s",
1013 operand, JNI_TRUE);
1014 exit(1);
1015 }
1016
1017 /*
1018 * Command line splash screen option should have precedence
1019 * over the manifest, so the manifest data is used only if
1020 * splash_file_name has not been initialized above during command
1021 * line parsing
1022 */
1023 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
1024 splash_file_name = info.splashscreen_image_file_name;
1025 splash_jar_name = operand;
1026 }
1027 } else {
1028 info.manifest_version = NULL;
1029 info.main_class = NULL;
1030 info.jre_version = NULL;
1031 info.jre_restrict_search = 0;
1032 }
1033
1034 /*
1035 * Passing on splash screen info in environment variables
1036 */
1037 if (splash_file_name && !headlessflag) {
1038 char* splash_file_entry = JLI_MemAlloc(strlen(SPLASH_FILE_ENV_ENTRY "=")+strlen(splash_file_name)+1);
1039 strcpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
1040 strcat(splash_file_entry, splash_file_name);
1041 putenv(splash_file_entry);
1042 }
1043 if (splash_jar_name && !headlessflag) {
1044 char* splash_jar_entry = JLI_MemAlloc(strlen(SPLASH_JAR_ENV_ENTRY "=")+strlen(splash_jar_name)+1);
1045 strcpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
1046 strcat(splash_jar_entry, splash_jar_name);
1047 putenv(splash_jar_entry);
1048 }
1049
1050 /*
1051 * The JRE-Version and JRE-Restrict-Search values (if any) from the
1052 * manifest are overwritten by any specified on the command line.
1053 */
1054 if (version != NULL)
1055 info.jre_version = version;
1056 if (restrict_search != -1)
1057 info.jre_restrict_search = restrict_search;
1058
1059 /*
1060 * "Valid" returns (other than unrecoverable errors) follow. Set
1061 * main_class as a side-effect of this routine.
1062 */
1063 if (info.main_class != NULL)
1064 *main_class = JLI_StringDup(info.main_class);
1065
1066 /*
1067 * If no version selection information is found either on the command
1068 * line or in the manifest, simply return.
1069 */
1070 if (info.jre_version == NULL) {
1071 JLI_FreeManifest();
1072 JLI_MemFree(new_argv);
1073 return;
1074 }
1075
1076 /*
1077 * Check for correct syntax of the version specification (JSR 56).
1078 */
1079 if (!JLI_ValidVersionString(info.jre_version)) {
1080 ReportErrorMessage2("Syntax error in version specification \"%s\"",
1081 info.jre_version, JNI_TRUE);
1082 exit(1);
1083 }
1084
1085 /*
1086 * Find the appropriate JVM on the system. Just to be as forgiving as
1087 * possible, if the standard algorithms don't locate an appropriate
1088 * jre, check to see if the one running will satisfy the requirements.
1089 * This can happen on systems which haven't been set-up for multiple
1090 * JRE support.
1091 */
1092 jre = LocateJRE(&info);
1093 if (_launcher_debug)
1094 printf("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
1095 (info.jre_version?info.jre_version:"null"),
1096 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
1097 if (jre == NULL) {
1098 if (JLI_AcceptableRelease(FULL_VERSION, info.jre_version)) {
1099 JLI_FreeManifest();
1100 JLI_MemFree(new_argv);
1101 return;
1102 } else {
1103 ReportErrorMessage2(
1104 "Unable to locate JRE meeting specification \"%s\"",
1105 info.jre_version, JNI_TRUE);
1106 exit(1);
1107 }
1108 }
1109
1110 /*
1111 * If I'm not the chosen one, exec the chosen one. Returning from
1112 * ExecJRE indicates that I am indeed the chosen one.
1113 *
1114 * The private environment variable _JAVA_VERSION_SET is used to
1115 * prevent the chosen one from re-reading the manifest file and
1116 * using the values found within to override the (potential) command
1117 * line flags stripped from argv (because the target may not
1118 * understand them). Passing the MainClass value is an optimization
1119 * to avoid locating, expanding and parsing the manifest extra
1120 * times.
1121 */
1122 if (info.main_class != NULL) {
1123 if (strlen(info.main_class) <= MAXNAMELEN) {
1124 (void)strcat(env_entry, info.main_class);
1125 } else {
1126 ReportErrorMessage("Error: main-class: attribute exceeds system limits\n", JNI_TRUE);
1127 exit(1);
1128 }
1129 }
1130 (void)putenv(env_entry);
1131 ExecJRE(jre, new_argv);
1132 JLI_FreeManifest();
1133 JLI_MemFree(new_argv);
1134 return;
1135}
1136
1137/*
1138 * Parses command line arguments. Returns JNI_FALSE if launcher
1139 * should exit without starting vm, returns JNI_TRUE if vm needs
1140 * to be started to process given options. *pret (the launcher
1141 * process return value) is set to 0 for a normal exit.
1142 */
1143static jboolean
1144ParseArguments(int *pargc, char ***pargv, char **pjarfile,
1145 char **pclassname, int *pret, const char *jvmpath)
1146{
1147 int argc = *pargc;
1148 char **argv = *pargv;
1149 jboolean jarflag = JNI_FALSE;
1150 char *arg;
1151
1152 *pret = 0;
1153
1154 while ((arg = *argv) != 0 && *arg == '-') {
1155 argv++; --argc;
1156 if (strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) {
1157 if (argc < 1) {
1158 ReportErrorMessage2("%s requires class path specification",
1159 arg, JNI_TRUE);
1160 printUsage = JNI_TRUE;
1161 *pret = 1;
1162 return JNI_TRUE;
1163 }
1164 SetClassPath(*argv);
1165 argv++; --argc;
1166 } else if (strcmp(arg, "-jar") == 0) {
1167 jarflag = JNI_TRUE;
1168 } else if (strcmp(arg, "-help") == 0 ||
1169 strcmp(arg, "-h") == 0 ||
1170 strcmp(arg, "-?") == 0) {
1171 printUsage = JNI_TRUE;
1172 return JNI_TRUE;
1173 } else if (strcmp(arg, "-version") == 0) {
1174 printVersion = JNI_TRUE;
1175 return JNI_TRUE;
1176 } else if (strcmp(arg, "-showversion") == 0) {
1177 showVersion = JNI_TRUE;
1178 } else if (strcmp(arg, "-X") == 0) {
1179 printXUsage = JNI_TRUE;
1180 return JNI_TRUE;
1181/*
1182 * The following case provide backward compatibility with old-style
1183 * command line options.
1184 */
1185 } else if (strcmp(arg, "-fullversion") == 0) {
1186 fprintf(stderr, "%s full version \"%s\"\n", launchername,
1187 FULL_VERSION);
1188 return JNI_FALSE;
1189 } else if (strcmp(arg, "-verbosegc") == 0) {
1190 AddOption("-verbose:gc", NULL);
1191 } else if (strcmp(arg, "-t") == 0) {
1192 AddOption("-Xt", NULL);
1193 } else if (strcmp(arg, "-tm") == 0) {
1194 AddOption("-Xtm", NULL);
1195 } else if (strcmp(arg, "-debug") == 0) {
1196 AddOption("-Xdebug", NULL);
1197 } else if (strcmp(arg, "-noclassgc") == 0) {
1198 AddOption("-Xnoclassgc", NULL);
1199 } else if (strcmp(arg, "-Xfuture") == 0) {
1200 AddOption("-Xverify:all", NULL);
1201 } else if (strcmp(arg, "-verify") == 0) {
1202 AddOption("-Xverify:all", NULL);
1203 } else if (strcmp(arg, "-verifyremote") == 0) {
1204 AddOption("-Xverify:remote", NULL);
1205 } else if (strcmp(arg, "-noverify") == 0) {
1206 AddOption("-Xverify:none", NULL);
1207 } else if (strcmp(arg, "-XXsuppressExitMessage") == 0) {
1208 noExitErrorMessage = 1;
1209 } else if (strncmp(arg, "-prof", 5) == 0) {
1210 char *p = arg + 5;
1211 char *tmp = JLI_MemAlloc(strlen(arg) + 50);
1212 if (*p) {
1213 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1214 } else {
1215 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1216 }
1217 AddOption(tmp, NULL);
1218 } else if (strncmp(arg, "-ss", 3) == 0 ||
1219 strncmp(arg, "-oss", 4) == 0 ||
1220 strncmp(arg, "-ms", 3) == 0 ||
1221 strncmp(arg, "-mx", 3) == 0) {
1222 char *tmp = JLI_MemAlloc(strlen(arg) + 6);
1223 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1224 AddOption(tmp, NULL);
1225 } else if (strcmp(arg, "-checksource") == 0 ||
1226 strcmp(arg, "-cs") == 0 ||
1227 strcmp(arg, "-noasyncgc") == 0) {
1228 /* No longer supported */
1229 fprintf(stderr,
1230 "Warning: %s option is no longer supported.\n",
1231 arg);
1232 } else if (strncmp(arg, "-version:", 9) == 0 ||
1233 strcmp(arg, "-no-jre-restrict-search") == 0 ||
1234 strcmp(arg, "-jre-restrict-search") == 0 ||
1235 strncmp(arg, "-splash:", 8) == 0) {
1236 ; /* Ignore machine independent options already handled */
1237 } else if (RemovableMachineDependentOption(arg) ) {
1238 ; /* Do not pass option to vm. */
1239 }
1240 else {
1241 AddOption(arg, NULL);
1242 }
1243 }
1244
1245 if (--argc >= 0) {
1246 if (jarflag) {
1247 *pjarfile = *argv++;
1248 *pclassname = 0;
1249 } else {
1250 *pjarfile = 0;
1251 *pclassname = *argv++;
1252 }
1253 *pargc = argc;
1254 *pargv = argv;
1255 }
1256
1257 return JNI_TRUE;
1258}
1259
1260/*
1261 * Initializes the Java Virtual Machine. Also frees options array when
1262 * finished.
1263 */
1264static jboolean
1265InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1266{
1267 JavaVMInitArgs args;
1268 jint r;
1269
1270 memset(&args, 0, sizeof(args));
1271 args.version = JNI_VERSION_1_2;
1272 args.nOptions = numOptions;
1273 args.options = options;
1274 args.ignoreUnrecognized = JNI_FALSE;
1275
1276 if (_launcher_debug) {
1277 int i = 0;
1278 printf("JavaVM args:\n ");
1279 printf("version 0x%08lx, ", (long)args.version);
1280 printf("ignoreUnrecognized is %s, ",
1281 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1282 printf("nOptions is %ld\n", (long)args.nOptions);
1283 for (i = 0; i < numOptions; i++)
1284 printf(" option[%2d] = '%s'\n",
1285 i, args.options[i].optionString);
1286 }
1287
1288 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1289 JLI_MemFree(options);
1290 return r == JNI_OK;
1291}
1292
1293#define JNI_ERROR "Error: A JNI error has occurred, please check your installation and try again"
1294
1295#define NULL_CHECK0(e) if ((e) == 0) { \
1296 ReportErrorMessage(JNI_ERROR, JNI_TRUE); \
1297 return 0; \
1298 }
1299
1300#define NULL_CHECK(e) if ((e) == 0) { \
1301 ReportErrorMessage(JNI_ERROR, JNI_TRUE); \
1302 return; \
1303 }
1304
1305static jstring platformEncoding = NULL;
1306static jstring getPlatformEncoding(JNIEnv *env) {
1307 if (platformEncoding == NULL) {
1308 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1309 if (propname) {
1310 jclass cls;
1311 jmethodID mid;
1312 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/lang/System"));
1313 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1314 env, cls,
1315 "getProperty",
1316 "(Ljava/lang/String;)Ljava/lang/String;"));
1317 platformEncoding = (*env)->CallStaticObjectMethod (
1318 env, cls, mid, propname);
1319 }
1320 }
1321 return platformEncoding;
1322}
1323
1324static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1325 jclass cls;
1326 jmethodID mid;
1327 NULL_CHECK0 (cls = (*env)->FindClass(env, "java/nio/charset/Charset"));
1328 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1329 env, cls,
1330 "isSupported",
1331 "(Ljava/lang/String;)Z"));
1332 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1333}
1334
1335/*
1336 * Returns a new Java string object for the specified platform string.
1337 */
1338static jstring
1339NewPlatformString(JNIEnv *env, char *s)
1340{
1341 int len = (int)strlen(s);
1342 jclass cls;
1343 jmethodID mid;
1344 jbyteArray ary;
1345 jstring enc;
1346
1347 if (s == NULL)
1348 return 0;
1349 enc = getPlatformEncoding(env);
1350
1351 ary = (*env)->NewByteArray(env, len);
1352 if (ary != 0) {
1353 jstring str = 0;
1354 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1355 if (!(*env)->ExceptionOccurred(env)) {
1356 if (isEncodingSupported(env, enc) == JNI_TRUE) {
1357 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1358 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1359 "([BLjava/lang/String;)V"));
1360 str = (*env)->NewObject(env, cls, mid, ary, enc);
1361 } else {
1362 /*If the encoding specified in sun.jnu.encoding is not
1363 endorsed by "Charset.isSupported" we have to fall back
1364 to use String(byte[]) explicitly here without specifying
1365 the encoding name, in which the StringCoding class will
1366 pickup the iso-8859-1 as the fallback converter for us.
1367 */
1368 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1369 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1370 "([B)V"));
1371 str = (*env)->NewObject(env, cls, mid, ary);
1372 }
1373 (*env)->DeleteLocalRef(env, ary);
1374 return str;
1375 }
1376 }
1377 return 0;
1378}
1379
1380/*
1381 * Returns a new array of Java string objects for the specified
1382 * array of platform strings.
1383 */
1384static jobjectArray
1385NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1386{
1387 jarray cls;
1388 jarray ary;
1389 int i;
1390
1391 NULL_CHECK0(cls = (*env)->FindClass(env, "java/lang/String"));
1392 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1393 for (i = 0; i < strc; i++) {
1394 jstring str = NewPlatformString(env, *strv++);
1395 NULL_CHECK0(str);
1396 (*env)->SetObjectArrayElement(env, ary, i, str);
1397 (*env)->DeleteLocalRef(env, str);
1398 }
1399 return ary;
1400}
1401
1402/*
1403 * Loads a class, convert the '.' to '/'.
1404 */
1405static jclass
1406LoadClass(JNIEnv *env, char *name)
1407{
1408 char *buf = JLI_MemAlloc(strlen(name) + 1);
1409 char *s = buf, *t = name, c;
1410 jclass cls;
1411 jlong start = 0, end;
1412
1413 if (_launcher_debug)
1414 start = CounterGet();
1415
1416 do {
1417 c = *t++;
1418 *s++ = (c == '.') ? '/' : c;
1419 } while (c != '\0');
1420 cls = (*env)->FindClass(env, buf);
1421 JLI_MemFree(buf);
1422
1423 if (_launcher_debug) {
1424 end = CounterGet();
1425 printf("%ld micro seconds to load main class\n",
1426 (long)(jint)Counter2Micros(end-start));
1427 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1428 }
1429
1430 return cls;
1431}
1432
1433
1434/*
1435 * Returns the main class name for the specified jar file.
1436 */
1437static jstring
1438GetMainClassName(JNIEnv *env, char *jarname)
1439{
1440#define MAIN_CLASS "Main-Class"
1441 jclass cls;
1442 jmethodID mid;
1443 jobject jar, man, attr;
1444 jstring str, result = 0;
1445
1446 NULL_CHECK0(cls = (*env)->FindClass(env, "java/util/jar/JarFile"));
1447 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1448 "(Ljava/lang/String;)V"));
1449 NULL_CHECK0(str = NewPlatformString(env, jarname));
1450 NULL_CHECK0(jar = (*env)->NewObject(env, cls, mid, str));
1451 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "getManifest",
1452 "()Ljava/util/jar/Manifest;"));
1453 man = (*env)->CallObjectMethod(env, jar, mid);
1454 if (man != 0) {
1455 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1456 (*env)->GetObjectClass(env, man),
1457 "getMainAttributes",
1458 "()Ljava/util/jar/Attributes;"));
1459 attr = (*env)->CallObjectMethod(env, man, mid);
1460 if (attr != 0) {
1461 NULL_CHECK0(mid = (*env)->GetMethodID(env,
1462 (*env)->GetObjectClass(env, attr),
1463 "getValue",
1464 "(Ljava/lang/String;)Ljava/lang/String;"));
1465 NULL_CHECK0(str = NewPlatformString(env, MAIN_CLASS));
1466 result = (*env)->CallObjectMethod(env, attr, mid, str);
1467 }
1468 }
1469 return result;
1470}
1471
1472#ifdef JAVA_ARGS
1473static char *java_args[] = JAVA_ARGS;
1474static char *app_classpath[] = APP_CLASSPATH;
1475
1476/*
1477 * For tools, convert command line args thus:
1478 * javac -cp foo:foo/"*" -J-ms32m ...
1479 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1480 */
1481static void
1482TranslateApplicationArgs(int *pargc, char ***pargv)
1483{
1484 const int NUM_ARGS = (sizeof(java_args) / sizeof(char *));
1485 int argc = *pargc;
1486 char **argv = *pargv;
1487 int nargc = argc + NUM_ARGS;
1488 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1489 int i;
1490
1491 *pargc = nargc;
1492 *pargv = nargv;
1493
1494 /* Copy the VM arguments (i.e. prefixed with -J) */
1495 for (i = 0; i < NUM_ARGS; i++) {
1496 char *arg = java_args[i];
1497 if (arg[0] == '-' && arg[1] == 'J') {
1498 *nargv++ = arg + 2;
1499 }
1500 }
1501
1502 for (i = 0; i < argc; i++) {
1503 char *arg = argv[i];
1504 if (arg[0] == '-' && arg[1] == 'J') {
1505 if (arg[2] == '\0') {
1506 ReportErrorMessage("Error: the -J option should not be "
1507 "followed by a space.", JNI_TRUE);
1508 exit(1);
1509 }
1510 *nargv++ = arg + 2;
1511 }
1512 }
1513
1514 /* Copy the rest of the arguments */
1515 for (i = 0; i < NUM_ARGS; i++) {
1516 char *arg = java_args[i];
1517 if (arg[0] != '-' || arg[1] != 'J') {
1518 *nargv++ = arg;
1519 }
1520 }
1521 for (i = 0; i < argc; i++) {
1522 char *arg = argv[i];
1523 if (arg[0] == '-') {
1524 if (arg[1] == 'J')
1525 continue;
1526#ifdef EXPAND_CLASSPATH_WILDCARDS
1527 if (arg[1] == 'c'
1528 && (strcmp(arg, "-cp") == 0 ||
1529 strcmp(arg, "-classpath") == 0)
1530 && i < argc - 1) {
1531 *nargv++ = arg;
1532 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1533 i++;
1534 continue;
1535 }
1536#endif
1537 }
1538 *nargv++ = arg;
1539 }
1540 *nargv = 0;
1541}
1542
1543/*
1544 * For our tools, we try to add 3 VM options:
1545 * -Denv.class.path=<envcp>
1546 * -Dapplication.home=<apphome>
1547 * -Djava.class.path=<appcp>
1548 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1549 * tells javac where to find binary classes through this environment
1550 * variable. Notice that users will be able to compile against our
1551 * tools classes (sun.tools.javac.Main) only if they explicitly add
1552 * tools.jar to CLASSPATH.
1553 * <apphome> is the directory where the application is installed.
1554 * <appcp> is the classpath to where our apps' classfiles are.
1555 */
1556static jboolean
1557AddApplicationOptions()
1558{
1559 const int NUM_APP_CLASSPATH = (sizeof(app_classpath) / sizeof(char *));
1560 char *envcp, *appcp, *apphome;
1561 char home[MAXPATHLEN]; /* application home */
1562 char separator[] = { PATH_SEPARATOR, '\0' };
1563 int size, i;
1564 int strlenHome;
1565
1566 {
1567 const char *s = getenv("CLASSPATH");
1568 if (s) {
1569 s = (char *) JLI_WildcardExpandClasspath(s);
1570 /* 40 for -Denv.class.path= */
1571 envcp = (char *)JLI_MemAlloc(strlen(s) + 40);
1572 sprintf(envcp, "-Denv.class.path=%s", s);
1573 AddOption(envcp, NULL);
1574 }
1575 }
1576
1577 if (!GetApplicationHome(home, sizeof(home))) {
1578 ReportErrorMessage("Can't determine application home", JNI_TRUE);
1579 return JNI_FALSE;
1580 }
1581
1582 /* 40 for '-Dapplication.home=' */
1583 apphome = (char *)JLI_MemAlloc(strlen(home) + 40);
1584 sprintf(apphome, "-Dapplication.home=%s", home);
1585 AddOption(apphome, NULL);
1586
1587 /* How big is the application's classpath? */
1588 size = 40; /* 40: "-Djava.class.path=" */
1589 strlenHome = (int)strlen(home);
1590 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
1591 size += strlenHome + (int)strlen(app_classpath[i]) + 1; /* 1: separator */
1592 }
1593 appcp = (char *)JLI_MemAlloc(size + 1);
1594 strcpy(appcp, "-Djava.class.path=");
1595 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
1596 strcat(appcp, home); /* c:\program files\myapp */
1597 strcat(appcp, app_classpath[i]); /* \lib\myapp.jar */
1598 strcat(appcp, separator); /* ; */
1599 }
1600 appcp[strlen(appcp)-1] = '\0'; /* remove trailing path separator */
1601 AddOption(appcp, NULL);
1602 return JNI_TRUE;
1603}
1604#endif /* JAVA_ARGS */
1605
1606/*
1607 * inject the -Dsun.java.command pseudo property into the args structure
1608 * this pseudo property is used in the HotSpot VM to expose the
1609 * Java class name and arguments to the main method to the VM. The
1610 * HotSpot VM uses this pseudo property to store the Java class name
1611 * (or jar file name) and the arguments to the class's main method
1612 * to the instrumentation memory region. The sun.java.command pseudo
1613 * property is not exported by HotSpot to the Java layer.
1614 */
1615void
1616SetJavaCommandLineProp(char *classname, char *jarfile,
1617 int argc, char **argv)
1618{
1619
1620 int i = 0;
1621 size_t len = 0;
1622 char* javaCommand = NULL;
1623 char* dashDstr = "-Dsun.java.command=";
1624
1625 if (classname == NULL && jarfile == NULL) {
1626 /* unexpected, one of these should be set. just return without
1627 * setting the property
1628 */
1629 return;
1630 }
1631
1632 /* if the class name is not set, then use the jarfile name */
1633 if (classname == NULL) {
1634 classname = jarfile;
1635 }
1636
1637 /* determine the amount of memory to allocate assuming
1638 * the individual components will be space separated
1639 */
1640 len = strlen(classname);
1641 for (i = 0; i < argc; i++) {
1642 len += strlen(argv[i]) + 1;
1643 }
1644
1645 /* allocate the memory */
1646 javaCommand = (char*) JLI_MemAlloc(len + strlen(dashDstr) + 1);
1647
1648 /* build the -D string */
1649 *javaCommand = '\0';
1650 strcat(javaCommand, dashDstr);
1651 strcat(javaCommand, classname);
1652
1653 for (i = 0; i < argc; i++) {
1654 /* the components of the string are space separated. In
1655 * the case of embedded white space, the relationship of
1656 * the white space separated components to their true
1657 * positional arguments will be ambiguous. This issue may
1658 * be addressed in a future release.
1659 */
1660 strcat(javaCommand, " ");
1661 strcat(javaCommand, argv[i]);
1662 }
1663
1664 AddOption(javaCommand, NULL);
1665}
1666
1667/*
1668 * JVM would like to know if it's created by a standard Sun launcher, or by
1669 * user native application, the following property indicates the former.
1670 */
1671void SetJavaLauncherProp() {
1672 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1673}
1674
1675/*
1676 * Prints the version information from the java.version and other properties.
1677 */
1678static void
1679PrintJavaVersion(JNIEnv *env)
1680{
1681 jclass ver;
1682 jmethodID print;
1683
1684 NULL_CHECK(ver = (*env)->FindClass(env, "sun/misc/Version"));
1685 NULL_CHECK(print = (*env)->GetStaticMethodID(env, ver, "print", "()V"));
1686
1687 (*env)->CallStaticVoidMethod(env, ver, print);
1688}
1689
1690/*
1691 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelp.java
1692 */
1693static void
1694PrintUsage(JNIEnv* env, jboolean doXUsage)
1695{
1696 jclass cls;
1697 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1698 jstring jprogname, vm1, vm2;
1699 int i;
1700
1701 NULL_CHECK(cls = (*env)->FindClass(env, "sun/launcher/LauncherHelp"));
1702
1703
1704 if (doXUsage) {
1705 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1706 "printXUsageMessage", "(Z)V"));
1707#ifdef __OS2__
1708 // printing usage info to stderr is not respected, use stdout
1709 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_FALSE);
1710#else
1711 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1712#endif
1713 } else {
1714 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1715 "initHelpMessage", "(Ljava/lang/String;)V"));
1716
1717 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1718 "(Ljava/lang/String;Ljava/lang/String;)V"));
1719
1720 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1721 "appendVmSynonymMessage",
1722 "(Ljava/lang/String;Ljava/lang/String;)V"));
1723 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1724 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1725
1726 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1727 "printHelpMessage", "(Z)V"));
1728
1729 jprogname = (*env)->NewStringUTF(env, progname);
1730
1731 /* Initialize the usage message with the usual preamble */
1732 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1733
1734
1735 /* Assemble the other variant part of the usage */
1736 if ((knownVMs[0].flag == VM_KNOWN) ||
1737 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1738 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1739 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1740 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1741 }
1742 for (i=1; i<knownVMsCount; i++) {
1743 if (knownVMs[i].flag == VM_KNOWN) {
1744 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1745 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1746 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1747 }
1748 }
1749 for (i=1; i<knownVMsCount; i++) {
1750 if (knownVMs[i].flag == VM_ALIASED_TO) {
1751 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1752 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1753 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1754 }
1755 }
1756
1757 /* The first known VM is the default */
1758 {
1759 jboolean isServerClassMachine = ServerClassMachine();
1760
1761 const char* defaultVM = knownVMs[0].name+1;
1762 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1763 defaultVM = knownVMs[0].server_class+1;
1764 }
1765
1766 vm1 = (*env)->NewStringUTF(env, defaultVM);
1767 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1768 }
1769
1770 /* Complete the usage message and print to stderr*/
1771#ifdef __OS2__
1772 // printing usage info to stderr is not respected, use stdout
1773 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_FALSE);
1774#else
1775 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1776#endif
1777 }
1778 return;
1779}
1780
1781/*
1782 * Read the jvm.cfg file and fill the knownJVMs[] array.
1783 *
1784 * The functionality of the jvm.cfg file is subject to change without
1785 * notice and the mechanism will be removed in the future.
1786 *
1787 * The lexical structure of the jvm.cfg file is as follows:
1788 *
1789 * jvmcfg := { vmLine }
1790 * vmLine := knownLine
1791 * | aliasLine
1792 * | warnLine
1793 * | ignoreLine
1794 * | errorLine
1795 * | predicateLine
1796 * | commentLine
1797 * knownLine := flag "KNOWN" EOL
1798 * warnLine := flag "WARN" EOL
1799 * ignoreLine := flag "IGNORE" EOL
1800 * errorLine := flag "ERROR" EOL
1801 * aliasLine := flag "ALIASED_TO" flag EOL
1802 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1803 * commentLine := "#" text EOL
1804 * flag := "-" identifier
1805 *
1806 * The semantics are that when someone specifies a flag on the command line:
1807 * - if the flag appears on a knownLine, then the identifier is used as
1808 * the name of the directory holding the JVM library (the name of the JVM).
1809 * - if the flag appears as the first flag on an aliasLine, the identifier
1810 * of the second flag is used as the name of the JVM.
1811 * - if the flag appears on a warnLine, the identifier is used as the
1812 * name of the JVM, but a warning is generated.
1813 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1814 * name of a JVM, but the identifier is ignored and the default vm used
1815 * - if the flag appears on an errorLine, an error is generated.
1816 * - if the flag appears as the first flag on a predicateLine, and
1817 * the machine on which you are running passes the predicate indicated,
1818 * then the identifier of the second flag is used as the name of the JVM,
1819 * otherwise the identifier of the first flag is used as the name of the JVM.
1820 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1821 * file determines the name of the JVM.
1822 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1823 * since they only make sense if someone hasn't specified the name of the
1824 * JVM on the command line.
1825 *
1826 * The intent of the jvm.cfg file is to allow several JVM libraries to
1827 * be installed in different subdirectories of a single JRE installation,
1828 * for space-savings and convenience in testing.
1829 * The intent is explicitly not to provide a full aliasing or predicate
1830 * mechanism.
1831 */
1832jint
1833ReadKnownVMs(const char *jrepath, char * arch, jboolean speculative)
1834{
1835 FILE *jvmCfg;
1836 char jvmCfgName[MAXPATHLEN+20];
1837 char line[MAXPATHLEN+20];
1838 int cnt = 0;
1839 int lineno = 0;
1840 jlong start = 0, end;
1841 int vmType;
1842 char *tmpPtr;
1843 char *altVMName = NULL;
1844 char *serverClassVMName = NULL;
1845 static char *whiteSpace = " \t";
1846 if (_launcher_debug) {
1847 start = CounterGet();
1848 }
1849
1850 strcpy(jvmCfgName, jrepath);
1851 strcat(jvmCfgName, FILESEP "lib" FILESEP);
1852 strcat(jvmCfgName, arch);
1853 strcat(jvmCfgName, FILESEP "jvm.cfg");
1854
1855 jvmCfg = fopen(jvmCfgName, "r");
1856 if (jvmCfg == NULL) {
1857 if (!speculative) {
1858 ReportErrorMessage2("Error: could not open `%s'", jvmCfgName,
1859 JNI_TRUE);
1860 exit(1);
1861 } else {
1862 return -1;
1863 }
1864 }
1865 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1866 vmType = VM_UNKNOWN;
1867 lineno++;
1868 if (line[0] == '#')
1869 continue;
1870 if (line[0] != '-') {
1871 fprintf(stderr, "Warning: no leading - on line %d of `%s'\n",
1872 lineno, jvmCfgName);
1873 }
1874 if (cnt >= knownVMsLimit) {
1875 GrowKnownVMs(cnt);
1876 }
1877 line[strlen(line)-1] = '\0'; /* remove trailing newline */
1878 tmpPtr = line + strcspn(line, whiteSpace);
1879 if (*tmpPtr == 0) {
1880 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
1881 lineno, jvmCfgName);
1882 } else {
1883 /* Null-terminate this string for JLI_StringDup below */
1884 *tmpPtr++ = 0;
1885 tmpPtr += strspn(tmpPtr, whiteSpace);
1886 if (*tmpPtr == 0) {
1887 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
1888 lineno, jvmCfgName);
1889 } else {
1890 if (!strncmp(tmpPtr, "KNOWN", strlen("KNOWN"))) {
1891 vmType = VM_KNOWN;
1892 } else if (!strncmp(tmpPtr, "ALIASED_TO", strlen("ALIASED_TO"))) {
1893 tmpPtr += strcspn(tmpPtr, whiteSpace);
1894 if (*tmpPtr != 0) {
1895 tmpPtr += strspn(tmpPtr, whiteSpace);
1896 }
1897 if (*tmpPtr == 0) {
1898 fprintf(stderr, "Warning: missing VM alias on line %d of `%s'\n",
1899 lineno, jvmCfgName);
1900 } else {
1901 /* Null terminate altVMName */
1902 altVMName = tmpPtr;
1903 tmpPtr += strcspn(tmpPtr, whiteSpace);
1904 *tmpPtr = 0;
1905 vmType = VM_ALIASED_TO;
1906 }
1907 } else if (!strncmp(tmpPtr, "WARN", strlen("WARN"))) {
1908 vmType = VM_WARN;
1909 } else if (!strncmp(tmpPtr, "IGNORE", strlen("IGNORE"))) {
1910 vmType = VM_IGNORE;
1911 } else if (!strncmp(tmpPtr, "ERROR", strlen("ERROR"))) {
1912 vmType = VM_ERROR;
1913 } else if (!strncmp(tmpPtr,
1914 "IF_SERVER_CLASS",
1915 strlen("IF_SERVER_CLASS"))) {
1916 tmpPtr += strcspn(tmpPtr, whiteSpace);
1917 if (*tmpPtr != 0) {
1918 tmpPtr += strspn(tmpPtr, whiteSpace);
1919 }
1920 if (*tmpPtr == 0) {
1921 fprintf(stderr, "Warning: missing server class VM on line %d of `%s'\n",
1922 lineno, jvmCfgName);
1923 } else {
1924 /* Null terminate server class VM name */
1925 serverClassVMName = tmpPtr;
1926 tmpPtr += strcspn(tmpPtr, whiteSpace);
1927 *tmpPtr = 0;
1928 vmType = VM_IF_SERVER_CLASS;
1929 }
1930 } else {
1931 fprintf(stderr, "Warning: unknown VM type on line %d of `%s'\n",
1932 lineno, &jvmCfgName[0]);
1933 vmType = VM_KNOWN;
1934 }
1935 }
1936 }
1937
1938 if (_launcher_debug)
1939 printf("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1940 if (vmType != VM_UNKNOWN) {
1941 knownVMs[cnt].name = JLI_StringDup(line);
1942 knownVMs[cnt].flag = vmType;
1943 switch (vmType) {
1944 default:
1945 break;
1946 case VM_ALIASED_TO:
1947 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1948 if (_launcher_debug) {
1949 printf(" name: %s vmType: %s alias: %s\n",
1950 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1951 }
1952 break;
1953 case VM_IF_SERVER_CLASS:
1954 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1955 if (_launcher_debug) {
1956 printf(" name: %s vmType: %s server_class: %s\n",
1957 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1958 }
1959 break;
1960 }
1961 cnt++;
1962 }
1963 }
1964 fclose(jvmCfg);
1965 knownVMsCount = cnt;
1966
1967 if (_launcher_debug) {
1968 end = CounterGet();
1969 printf("%ld micro seconds to parse jvm.cfg\n",
1970 (long)(jint)Counter2Micros(end-start));
1971 }
1972
1973 return cnt;
1974}
1975
1976
1977static void
1978GrowKnownVMs(int minimum)
1979{
1980 struct vmdesc* newKnownVMs;
1981 int newMax;
1982
1983 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1984 if (newMax <= minimum) {
1985 newMax = minimum;
1986 }
1987 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1988 if (knownVMs != NULL) {
1989 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1990 }
1991 JLI_MemFree(knownVMs);
1992 knownVMs = newKnownVMs;
1993 knownVMsLimit = newMax;
1994}
1995
1996
1997/* Returns index of VM or -1 if not found */
1998static int
1999KnownVMIndex(const char* name)
2000{
2001 int i;
2002 if (strncmp(name, "-J", 2) == 0) name += 2;
2003 for (i = 0; i < knownVMsCount; i++) {
2004 if (!strcmp(name, knownVMs[i].name)) {
2005 return i;
2006 }
2007 }
2008 return -1;
2009}
2010
2011static void
2012FreeKnownVMs()
2013{
2014 int i;
2015 for (i = 0; i < knownVMsCount; i++) {
2016 JLI_MemFree(knownVMs[i].name);
2017 knownVMs[i].name = NULL;
2018 }
2019 JLI_MemFree(knownVMs);
2020}
2021
2022
2023/*
2024 * Displays the splash screen according to the jar file name
2025 * and image file names stored in environment variables
2026 */
2027static void
2028ShowSplashScreen()
2029{
2030 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
2031 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
2032 int data_size;
2033 void *image_data;
2034 if (jar_name) {
2035 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
2036 if (image_data) {
2037 DoSplashInit();
2038 DoSplashLoadMemory(image_data, data_size);
2039 JLI_MemFree(image_data);
2040 }
2041 } else if (file_name) {
2042 DoSplashInit();
2043 DoSplashLoadFile(file_name);
2044 } else {
2045 return;
2046 }
2047 DoSplashSetFileJarName(file_name, jar_name);
2048}
Note: See TracBrowser for help on using the repository browser.