blob: b8c0382b1d18c76ef98baaa7fac82e07c0b739b5 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksrini16238472008-04-10 09:02:22 -07002 * Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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. 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.
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 RemovableOption.
53 */
54
55
56#include "java.h"
57
58/*
59 * A NOTE TO DEVELOPERS: For performance reasons it is important that
60 * the program image remain relatively small until after SelectVersion
61 * CreateExecutionEnvironment have finished their possibly recursive
62 * processing. Watch everything, but resist all temptations to use Java
63 * interfaces.
64 */
65
66static jboolean printVersion = JNI_FALSE; /* print and exit */
67static jboolean showVersion = JNI_FALSE; /* print but continue */
68static jboolean printUsage = JNI_FALSE; /* print and exit*/
69static jboolean printXUsage = JNI_FALSE; /* print and exit*/
70
71static const char *_program_name;
72static const char *_launcher_name;
73static jboolean _is_java_args = JNI_FALSE;
74static const char *_fVersion;
75static const char *_dVersion;
76static jboolean _wc_enabled = JNI_FALSE;
77static jint _ergo_policy = DEFAULT_POLICY;
78
79/*
80 * Entries for splash screen environment variables.
81 * putenv is performed in SelectVersion. We need
82 * them in memory until UnsetEnv, so they are made static
83 * global instead of auto local.
84 */
85static char* splash_file_entry = NULL;
86static char* splash_jar_entry = NULL;
87
88/*
89 * List of VM options to be specified when the VM is created.
90 */
91static JavaVMOption *options;
92static int numOptions, maxOptions;
93
94/*
95 * Prototypes for functions internal to launcher.
96 */
97static void SetClassPath(const char *s);
98static void SelectVersion(int argc, char **argv, char **main_class);
99static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
100 char **pclassname, int *pret, const char *jvmpath);
101static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
102 InvocationFunctions *ifn);
103static jstring NewPlatformString(JNIEnv *env, char *s);
104static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
ksrini20a64b22008-09-24 15:07:41 -0700105static jclass LoadMainClass(JNIEnv *env, jboolean isJar, char *name);
duke6e45e102007-12-01 00:00:00 +0000106
107static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
108static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
109static void SetApplicationClassPath(const char**);
110
111static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
112static void PrintUsage(JNIEnv* env, jboolean doXUsage);
113
114static void SetPaths(int argc, char **argv);
115
116static void DumpState();
117static jboolean RemovableOption(char *option);
118
119/* Maximum supported entries from jvm.cfg. */
120#define INIT_MAX_KNOWN_VMS 10
121
122/* Values for vmdesc.flag */
123enum vmdesc_flag {
124 VM_UNKNOWN = -1,
125 VM_KNOWN,
126 VM_ALIASED_TO,
127 VM_WARN,
128 VM_ERROR,
129 VM_IF_SERVER_CLASS,
130 VM_IGNORE
131};
132
133struct vmdesc {
134 char *name;
135 int flag;
136 char *alias;
137 char *server_class;
138};
139static struct vmdesc *knownVMs = NULL;
140static int knownVMsCount = 0;
141static int knownVMsLimit = 0;
142
143static void GrowKnownVMs();
144static int KnownVMIndex(const char* name);
145static void FreeKnownVMs();
146static void ShowSplashScreen();
147static jboolean IsWildCardEnabled();
148
149#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700150 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000151 printUsage = JNI_TRUE; \
152 *pret = 1; \
153 return JNI_TRUE; \
154}
155
156/*
157 * Running Java code in primordial thread caused many problems. We will
158 * create a new thread to invoke JVM. See 6316197 for more information.
159 */
160static jlong threadStackSize = 0; /* stack size of the new thread */
161
162int JNICALL JavaMain(void * args); /* entry point */
163
164typedef struct {
165 int argc;
166 char ** argv;
167 char * jarfile;
168 char * classname;
169 InvocationFunctions ifn;
170} JavaMainArgs;
171
172/*
173 * Entry point.
174 */
175int
176JLI_Launch(int argc, char ** argv, /* main argc, argc */
177 int jargc, const char** jargv, /* java args */
178 int appclassc, const char** appclassv, /* app classpath */
179 const char* fullversion, /* full version defined */
180 const char* dotversion, /* dot version defined */
181 const char* pname, /* program name */
182 const char* lname, /* launcher name */
183 jboolean javaargs, /* JAVA_ARGS */
184 jboolean cpwildcard, /* classpath wildcard*/
185 jboolean javaw, /* windows-only javaw */
186 jint ergo /* ergonomics class policy */
187)
188{
189 char *jarfile = 0;
190 char *classname = 0;
191 char *cpath = 0;
192 char *main_class = NULL;
193 int ret;
194 InvocationFunctions ifn;
195 jlong start, end;
196 char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];
197 char ** original_argv = argv;
198
199 _fVersion = fullversion;
200 _dVersion = dotversion;
201 _launcher_name = lname;
202 _program_name = pname;
203 _is_java_args = javaargs;
204 _wc_enabled = cpwildcard;
205 _ergo_policy = ergo;
206
ksrini52cded22008-03-06 07:51:28 -0800207 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000208 DumpState();
209
210 /*
211 * Make sure the specified version of the JRE is running.
212 *
213 * There are three things to note about the SelectVersion() routine:
214 * 1) If the version running isn't correct, this routine doesn't
215 * return (either the correct version has been exec'd or an error
216 * was issued).
217 * 2) Argc and Argv in this scope are *not* altered by this routine.
218 * It is the responsibility of subsequent code to ignore the
219 * arguments handled by this routine.
220 * 3) As a side-effect, the variable "main_class" is guaranteed to
221 * be set (if it should ever be set). This isn't exactly the
222 * poster child for structured programming, but it is a small
223 * price to pay for not processing a jar file operand twice.
224 * (Note: This side effect has been disabled. See comment on
225 * bugid 5030265 below.)
226 */
227 SelectVersion(argc, argv, &main_class);
228
229 /* copy original argv */
230 JLI_TraceLauncher("Command line Args:\n");
231 original_argv = (JLI_CopyArgs(argc, (const char**)argv));
232
233 CreateExecutionEnvironment(&argc, &argv,
234 jrepath, sizeof(jrepath),
235 jvmpath, sizeof(jvmpath),
236 original_argv);
237
238 ifn.CreateJavaVM = 0;
239 ifn.GetDefaultJavaVMInitArgs = 0;
240
241 if (JLI_IsTraceLauncher()) {
242 start = CounterGet();
243 }
244
245 if (!LoadJavaVM(jvmpath, &ifn)) {
246 return(6);
247 }
248
249 if (JLI_IsTraceLauncher()) {
250 end = CounterGet();
251 }
252
253 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
254 (long)(jint)Counter2Micros(end-start));
255
256 ++argv;
257 --argc;
258
259 if (IsJavaArgs()) {
260 /* Preprocess wrapper arguments */
261 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
262 if (!AddApplicationOptions(appclassc, appclassv)) {
263 return(1);
264 }
265 } else {
266 /* Set default CLASSPATH */
267 cpath = getenv("CLASSPATH");
268 if (cpath == NULL) {
269 cpath = ".";
270 }
271 SetClassPath(cpath);
272 }
273
274 /*
275 * Parse command line options; if the return value of
276 * ParseArguments is false, the program should exit.
277 */
278 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
279 return(ret);
280 }
281
282 /* Override class path if -jar flag was specified */
283 if (jarfile != 0) {
284 SetClassPath(jarfile);
285 }
286
287 /* set the -Dsun.java.command pseudo property */
288 SetJavaCommandLineProp(classname, jarfile, argc, argv);
289
290 /* Set the -Dsun.java.launcher pseudo property */
291 SetJavaLauncherProp();
292
293 /* set the -Dsun.java.launcher.* platform properties */
294 SetJavaLauncherPlatformProps();
295
296 /* Show the splash screen if needed */
297 ShowSplashScreen();
298
299 return ContinueInNewThread(&ifn, argc, argv, jarfile, classname, ret);
300
301}
302
ksrini20a64b22008-09-24 15:07:41 -0700303#define CHECK_EXCEPTION_NULL_LEAVE(e) \
304 if ((*env)->ExceptionOccurred(env)) { \
305 JLI_ReportExceptionDescription(env); \
306 goto leave; \
307 } \
308 if ((e) == NULL) { \
309 JLI_ReportErrorMessage(JNI_ERROR); \
310 goto leave; \
311 }
312
313#define CHECK_EXCEPTION_LEAVE(rv) \
314 if ((*env)->ExceptionOccurred(env)) { \
315 JLI_ReportExceptionDescription(env); \
316 ret = (rv); \
317 goto leave; \
318 }
duke6e45e102007-12-01 00:00:00 +0000319
320int JNICALL
321JavaMain(void * _args)
322{
323 JavaMainArgs *args = (JavaMainArgs *)_args;
324 int argc = args->argc;
325 char **argv = args->argv;
326 char *jarfile = args->jarfile;
327 char *classname = args->classname;
328 InvocationFunctions ifn = args->ifn;
329
330 JavaVM *vm = 0;
331 JNIEnv *env = 0;
332 jstring mainClassName;
333 jclass mainClass;
334 jmethodID mainID;
335 jobjectArray mainArgs;
336 int ret = 0;
337 jlong start, end;
338
duke6e45e102007-12-01 00:00:00 +0000339 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000340 start = CounterGet();
341 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700342 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000343 exit(1);
344 }
345
346 if (printVersion || showVersion) {
347 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700348 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000349 if (printVersion) {
350 ret = 0;
351 goto leave;
352 }
353 }
354
355 /* If the user specified neither a class name nor a JAR file */
356 if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
357 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700358 CHECK_EXCEPTION_LEAVE(1);
duke6e45e102007-12-01 00:00:00 +0000359 goto leave;
360 }
361
362 FreeKnownVMs(); /* after last possible PrintUsage() */
363
364 if (JLI_IsTraceLauncher()) {
365 end = CounterGet();
366 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
367 (long)(jint)Counter2Micros(end-start));
368 }
369
370 /* At this stage, argc/argv have the applications' arguments */
371 if (JLI_IsTraceLauncher()){
372 int i;
373 printf("Main-Class is '%s'\n", classname ? classname : "");
374 printf("Apps' argc is %d\n", argc);
375 for (i=0; i < argc; i++) {
376 printf(" argv[%2d] = '%s'\n", i, argv[i]);
377 }
378 }
379
380 ret = 1;
381
382 /*
383 * Get the application's main class.
384 *
385 * See bugid 5030265. The Main-Class name has already been parsed
386 * from the manifest, but not parsed properly for UTF-8 support.
387 * Hence the code here ignores the value previously extracted and
388 * uses the pre-existing code to reextract the value. This is
389 * possibly an end of release cycle expedient. However, it has
390 * also been discovered that passing some character sets through
391 * the environment has "strange" behavior on some variants of
392 * Windows. Hence, maybe the manifest parsing code local to the
393 * launcher should never be enhanced.
394 *
395 * Hence, future work should either:
396 * 1) Correct the local parsing code and verify that the
397 * Main-Class attribute gets properly passed through
398 * all environments,
399 * 2) Remove the vestages of maintaining main_class through
400 * the environment (and remove these comments).
401 */
402 if (jarfile != 0) {
ksrini20a64b22008-09-24 15:07:41 -0700403 mainClass = LoadMainClass(env, JNI_TRUE, jarfile);
duke6e45e102007-12-01 00:00:00 +0000404 } else {
ksrini20a64b22008-09-24 15:07:41 -0700405 mainClass = LoadMainClass(env, JNI_FALSE, classname);
duke6e45e102007-12-01 00:00:00 +0000406 }
ksrini20a64b22008-09-24 15:07:41 -0700407 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
duke6e45e102007-12-01 00:00:00 +0000408
ksrini20a64b22008-09-24 15:07:41 -0700409 /*
410 * The LoadMainClass not only loads the main class, it will also ensure
411 * that the main method's signature is correct, therefore further checking
412 * is not required. The main method is invoked here so that extraneous java
413 * stacks are not in the application stack trace.
414 */
duke6e45e102007-12-01 00:00:00 +0000415 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
416 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700417 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000418
419 /* Build argument array */
420 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700421 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000422
423 /* Invoke main method. */
424 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
425
426 /*
427 * The launcher's exit code (in the absence of calls to
428 * System.exit) will be non-zero if main threw an exception.
429 */
430 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
431
ksrini20a64b22008-09-24 15:07:41 -0700432leave:
duke6e45e102007-12-01 00:00:00 +0000433 /*
ksrini20a64b22008-09-24 15:07:41 -0700434 * Always detach the main thread so that it appears to have ended when
duke6e45e102007-12-01 00:00:00 +0000435 * the application's main method exits. This will invoke the
436 * uncaught exception handler machinery if main threw an
437 * exception. An uncaught exception handler cannot change the
438 * launcher's return code except by calling System.exit.
439 */
440 if ((*vm)->DetachCurrentThread(vm) != 0) {
ksrini0e817162008-08-26 10:21:20 -0700441 JLI_ReportErrorMessage(JVM_ERROR2);
duke6e45e102007-12-01 00:00:00 +0000442 ret = 1;
duke6e45e102007-12-01 00:00:00 +0000443 }
duke6e45e102007-12-01 00:00:00 +0000444 /*
445 * Wait for all non-daemon threads to end, then destroy the VM.
446 * This will actually create a trivial new Java waiter thread
447 * named "DestroyJavaVM", but this will be seen as a different
448 * thread from the one that executed main, even though they are
449 * the same C thread. This allows mainThread.join() and
450 * mainThread.isAlive() to work as expected.
451 */
452 (*vm)->DestroyJavaVM(vm);
453
454 return ret;
455}
456
duke6e45e102007-12-01 00:00:00 +0000457/*
458 * Checks the command line options to find which JVM type was
459 * specified. If no command line option was given for the JVM type,
460 * the default type is used. The environment variable
461 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
462 * checked as ways of specifying which JVM type to invoke.
463 */
464char *
465CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
466 int i, argi;
467 int argc;
468 char **newArgv;
469 int newArgvIdx = 0;
470 int isVMType;
471 int jvmidx = -1;
472 char *jvmtype = getenv("JDK_ALTERNATE_VM");
473
474 argc = *pargc;
475
476 /* To make things simpler we always copy the argv array */
477 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
478
479 /* The program name is always present */
480 newArgv[newArgvIdx++] = (*argv)[0];
481
482 for (argi = 1; argi < argc; argi++) {
483 char *arg = (*argv)[argi];
484 isVMType = 0;
485
486 if (IsJavaArgs()) {
487 if (arg[0] != '-') {
488 newArgv[newArgvIdx++] = arg;
489 continue;
490 }
491 } else {
492 if (JLI_StrCmp(arg, "-classpath") == 0 ||
493 JLI_StrCmp(arg, "-cp") == 0) {
494 newArgv[newArgvIdx++] = arg;
495 argi++;
496 if (argi < argc) {
497 newArgv[newArgvIdx++] = (*argv)[argi];
498 }
499 continue;
500 }
501 if (arg[0] != '-') break;
502 }
503
504 /* Did the user pass an explicit VM type? */
505 i = KnownVMIndex(arg);
506 if (i >= 0) {
507 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
508 isVMType = 1;
509 *pargc = *pargc - 1;
510 }
511
512 /* Did the user specify an "alternate" VM? */
513 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
514 isVMType = 1;
515 jvmtype = arg+((arg[1]=='X')? 10 : 12);
516 jvmidx = -1;
517 }
518
519 if (!isVMType) {
520 newArgv[newArgvIdx++] = arg;
521 }
522 }
523
524 /*
525 * Finish copying the arguments if we aborted the above loop.
526 * NOTE that if we aborted via "break" then we did NOT copy the
527 * last argument above, and in addition argi will be less than
528 * argc.
529 */
530 while (argi < argc) {
531 newArgv[newArgvIdx++] = (*argv)[argi];
532 argi++;
533 }
534
535 /* argv is null-terminated */
536 newArgv[newArgvIdx] = 0;
537
538 /* Copy back argv */
539 *argv = newArgv;
540 *pargc = newArgvIdx;
541
542 /* use the default VM type if not specified (no alias processing) */
543 if (jvmtype == NULL) {
544 char* result = knownVMs[0].name+1;
545 /* Use a different VM type if we are on a server class machine? */
546 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
547 (ServerClassMachine() == JNI_TRUE)) {
548 result = knownVMs[0].server_class+1;
549 }
550 JLI_TraceLauncher("Default VM: %s\n", result);
551 return result;
552 }
553
554 /* if using an alternate VM, no alias processing */
555 if (jvmidx < 0)
556 return jvmtype;
557
558 /* Resolve aliases first */
559 {
560 int loopCount = 0;
561 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
562 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
563
564 if (loopCount > knownVMsCount) {
565 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700566 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000567 exit(1);
568 } else {
569 return "ERROR";
570 /* break; */
571 }
572 }
573
574 if (nextIdx < 0) {
575 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700576 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000577 exit(1);
578 } else {
579 return "ERROR";
580 }
581 }
582 jvmidx = nextIdx;
583 jvmtype = knownVMs[jvmidx].name+1;
584 loopCount++;
585 }
586 }
587
588 switch (knownVMs[jvmidx].flag) {
589 case VM_WARN:
590 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700591 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000592 }
593 /* fall through */
594 case VM_IGNORE:
595 jvmtype = knownVMs[jvmidx=0].name + 1;
596 /* fall through */
597 case VM_KNOWN:
598 break;
599 case VM_ERROR:
600 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700601 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000602 exit(1);
603 } else {
604 return "ERROR";
605 }
606 }
607
608 return jvmtype;
609}
610
611/* copied from HotSpot function "atomll()" */
612static int
613parse_stack_size(const char *s, jlong *result) {
614 jlong n = 0;
615 int args_read = sscanf(s, jlong_format_specifier(), &n);
616 if (args_read != 1) {
617 return 0;
618 }
619 while (*s != '\0' && *s >= '0' && *s <= '9') {
620 s++;
621 }
622 // 4705540: illegal if more characters are found after the first non-digit
623 if (JLI_StrLen(s) > 1) {
624 return 0;
625 }
626 switch (*s) {
627 case 'T': case 't':
628 *result = n * GB * KB;
629 return 1;
630 case 'G': case 'g':
631 *result = n * GB;
632 return 1;
633 case 'M': case 'm':
634 *result = n * MB;
635 return 1;
636 case 'K': case 'k':
637 *result = n * KB;
638 return 1;
639 case '\0':
640 *result = n;
641 return 1;
642 default:
643 /* Create JVM with default stack and let VM handle malformed -Xss string*/
644 return 0;
645 }
646}
647
648/*
649 * Adds a new VM option with the given given name and value.
650 */
651void
652AddOption(char *str, void *info)
653{
654 /*
655 * Expand options array if needed to accommodate at least one more
656 * VM option.
657 */
658 if (numOptions >= maxOptions) {
659 if (options == 0) {
660 maxOptions = 4;
661 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
662 } else {
663 JavaVMOption *tmp;
664 maxOptions *= 2;
665 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
666 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
667 JLI_MemFree(options);
668 options = tmp;
669 }
670 }
671 options[numOptions].optionString = str;
672 options[numOptions++].extraInfo = info;
673
674 if (JLI_StrCCmp(str, "-Xss") == 0) {
675 jlong tmp;
676 if (parse_stack_size(str + 4, &tmp)) {
677 threadStackSize = tmp;
678 }
679 }
680}
681
682static void
683SetClassPath(const char *s)
684{
685 char *def;
martinc0ca3352009-06-22 16:41:27 -0700686 const char *orig = s;
687 static const char format[] = "-Djava.class.path=%s";
duke6e45e102007-12-01 00:00:00 +0000688 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700689 def = JLI_MemAlloc(sizeof(format)
690 - 2 /* strlen("%s") */
691 + JLI_StrLen(s));
692 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000693 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700694 if (s != orig)
695 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000696}
697
698/*
699 * The SelectVersion() routine ensures that an appropriate version of
700 * the JRE is running. The specification for the appropriate version
701 * is obtained from either the manifest of a jar file (preferred) or
702 * from command line options.
703 * The routine also parses splash screen command line options and
704 * passes on their values in private environment variables.
705 */
706static void
707SelectVersion(int argc, char **argv, char **main_class)
708{
709 char *arg;
710 char **new_argv;
711 char **new_argp;
712 char *operand;
713 char *version = NULL;
714 char *jre = NULL;
715 int jarflag = 0;
716 int headlessflag = 0;
717 int restrict_search = -1; /* -1 implies not known */
718 manifest_info info;
719 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
720 char *splash_file_name = NULL;
721 char *splash_jar_name = NULL;
722 char *env_in;
723 int res;
724
725 /*
726 * If the version has already been selected, set *main_class
727 * with the value passed through the environment (if any) and
728 * simply return.
729 */
730 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
731 if (*env_in != '\0')
732 *main_class = JLI_StringDup(env_in);
733 return;
734 }
735
736 /*
737 * Scan through the arguments for options relevant to multiple JRE
738 * support. For reference, the command line syntax is defined as:
739 *
740 * SYNOPSIS
741 * java [options] class [argument...]
742 *
743 * java [options] -jar file.jar [argument...]
744 *
745 * As the scan is performed, make a copy of the argument list with
746 * the version specification options (new to 1.5) removed, so that
747 * a version less than 1.5 can be exec'd.
748 *
749 * Note that due to the syntax of the native Windows interface
750 * CreateProcess(), processing similar to the following exists in
751 * the Windows platform specific routine ExecJRE (in java_md.c).
752 * Changes here should be reproduced there.
753 */
754 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
755 new_argv[0] = argv[0];
756 new_argp = &new_argv[1];
757 argc--;
758 argv++;
759 while ((arg = *argv) != 0 && *arg == '-') {
760 if (JLI_StrCCmp(arg, "-version:") == 0) {
761 version = arg + 9;
762 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
763 restrict_search = 1;
764 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
765 restrict_search = 0;
766 } else {
767 if (JLI_StrCmp(arg, "-jar") == 0)
768 jarflag = 1;
769 /* deal with "unfortunate" classpath syntax */
770 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
771 (argc >= 2)) {
772 *new_argp++ = arg;
773 argc--;
774 argv++;
775 arg = *argv;
776 }
777
778 /*
779 * Checking for headless toolkit option in the some way as AWT does:
780 * "true" means true and any other value means false
781 */
782 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
783 headlessflag = 1;
784 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
785 headlessflag = 0;
786 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
787 splash_file_name = arg+8;
788 }
789 *new_argp++ = arg;
790 }
791 argc--;
792 argv++;
793 }
794 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
795 operand = NULL;
796 } else {
797 argc--;
798 *new_argp++ = operand = *argv++;
799 }
800 while (argc-- > 0) /* Copy over [argument...] */
801 *new_argp++ = *argv++;
802 *new_argp = NULL;
803
804 /*
805 * If there is a jar file, read the manifest. If the jarfile can't be
806 * read, the manifest can't be read from the jar file, or the manifest
807 * is corrupt, issue the appropriate error messages and exit.
808 *
809 * Even if there isn't a jar file, construct a manifest_info structure
810 * containing the command line information. It's a convenient way to carry
811 * this data around.
812 */
813 if (jarflag && operand) {
814 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
815 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700816 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000817 else
ksrini0e817162008-08-26 10:21:20 -0700818 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000819 exit(1);
820 }
821
822 /*
823 * Command line splash screen option should have precedence
824 * over the manifest, so the manifest data is used only if
825 * splash_file_name has not been initialized above during command
826 * line parsing
827 */
828 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
829 splash_file_name = info.splashscreen_image_file_name;
830 splash_jar_name = operand;
831 }
832 } else {
833 info.manifest_version = NULL;
834 info.main_class = NULL;
835 info.jre_version = NULL;
836 info.jre_restrict_search = 0;
837 }
838
839 /*
840 * Passing on splash screen info in environment variables
841 */
842 if (splash_file_name && !headlessflag) {
843 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
844 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
845 JLI_StrCat(splash_file_entry, splash_file_name);
846 putenv(splash_file_entry);
847 }
848 if (splash_jar_name && !headlessflag) {
849 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
850 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
851 JLI_StrCat(splash_jar_entry, splash_jar_name);
852 putenv(splash_jar_entry);
853 }
854
855 /*
856 * The JRE-Version and JRE-Restrict-Search values (if any) from the
857 * manifest are overwritten by any specified on the command line.
858 */
859 if (version != NULL)
860 info.jre_version = version;
861 if (restrict_search != -1)
862 info.jre_restrict_search = restrict_search;
863
864 /*
865 * "Valid" returns (other than unrecoverable errors) follow. Set
866 * main_class as a side-effect of this routine.
867 */
868 if (info.main_class != NULL)
869 *main_class = JLI_StringDup(info.main_class);
870
871 /*
872 * If no version selection information is found either on the command
873 * line or in the manifest, simply return.
874 */
875 if (info.jre_version == NULL) {
876 JLI_FreeManifest();
877 JLI_MemFree(new_argv);
878 return;
879 }
880
881 /*
882 * Check for correct syntax of the version specification (JSR 56).
883 */
884 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700885 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000886 exit(1);
887 }
888
889 /*
890 * Find the appropriate JVM on the system. Just to be as forgiving as
891 * possible, if the standard algorithms don't locate an appropriate
892 * jre, check to see if the one running will satisfy the requirements.
893 * This can happen on systems which haven't been set-up for multiple
894 * JRE support.
895 */
896 jre = LocateJRE(&info);
897 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
898 (info.jre_version?info.jre_version:"null"),
899 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
900
901 if (jre == NULL) {
902 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
903 JLI_FreeManifest();
904 JLI_MemFree(new_argv);
905 return;
906 } else {
ksrini0e817162008-08-26 10:21:20 -0700907 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000908 exit(1);
909 }
910 }
911
912 /*
913 * If I'm not the chosen one, exec the chosen one. Returning from
914 * ExecJRE indicates that I am indeed the chosen one.
915 *
916 * The private environment variable _JAVA_VERSION_SET is used to
917 * prevent the chosen one from re-reading the manifest file and
918 * using the values found within to override the (potential) command
919 * line flags stripped from argv (because the target may not
920 * understand them). Passing the MainClass value is an optimization
921 * to avoid locating, expanding and parsing the manifest extra
922 * times.
923 */
ksrini8760ca92008-09-04 09:43:32 -0700924 if (info.main_class != NULL) {
925 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
926 (void)JLI_StrCat(env_entry, info.main_class);
927 } else {
asaha80ccd422009-04-16 21:08:04 -0700928 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700929 exit(1);
930 }
931 }
duke6e45e102007-12-01 00:00:00 +0000932 (void)putenv(env_entry);
933 ExecJRE(jre, new_argv);
934 JLI_FreeManifest();
935 JLI_MemFree(new_argv);
936 return;
937}
938
939/*
940 * Parses command line arguments. Returns JNI_FALSE if launcher
941 * should exit without starting vm, returns JNI_TRUE if vm needs
942 * to be started to process given options. *pret (the launcher
943 * process return value) is set to 0 for a normal exit.
944 */
945static jboolean
946ParseArguments(int *pargc, char ***pargv, char **pjarfile,
947 char **pclassname, int *pret, const char *jvmpath)
948{
949 int argc = *pargc;
950 char **argv = *pargv;
951 jboolean jarflag = JNI_FALSE;
952 char *arg;
953
954 *pret = 0;
955
956 while ((arg = *argv) != 0 && *arg == '-') {
957 argv++; --argc;
958 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
959 ARG_CHECK (argc, ARG_ERROR1, arg);
960 SetClassPath(*argv);
961 argv++; --argc;
962 } else if (JLI_StrCmp(arg, "-jar") == 0) {
963 ARG_CHECK (argc, ARG_ERROR2, arg);
964 jarflag = JNI_TRUE;
965 } else if (JLI_StrCmp(arg, "-help") == 0 ||
966 JLI_StrCmp(arg, "-h") == 0 ||
967 JLI_StrCmp(arg, "-?") == 0) {
968 printUsage = JNI_TRUE;
969 return JNI_TRUE;
970 } else if (JLI_StrCmp(arg, "-version") == 0) {
971 printVersion = JNI_TRUE;
972 return JNI_TRUE;
973 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
974 showVersion = JNI_TRUE;
975 } else if (JLI_StrCmp(arg, "-X") == 0) {
976 printXUsage = JNI_TRUE;
977 return JNI_TRUE;
978/*
979 * The following case provide backward compatibility with old-style
980 * command line options.
981 */
982 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -0700983 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +0000984 return JNI_FALSE;
985 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
986 AddOption("-verbose:gc", NULL);
987 } else if (JLI_StrCmp(arg, "-t") == 0) {
988 AddOption("-Xt", NULL);
989 } else if (JLI_StrCmp(arg, "-tm") == 0) {
990 AddOption("-Xtm", NULL);
991 } else if (JLI_StrCmp(arg, "-debug") == 0) {
992 AddOption("-Xdebug", NULL);
993 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
994 AddOption("-Xnoclassgc", NULL);
995 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
996 AddOption("-Xverify:all", NULL);
997 } else if (JLI_StrCmp(arg, "-verify") == 0) {
998 AddOption("-Xverify:all", NULL);
999 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1000 AddOption("-Xverify:remote", NULL);
1001 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1002 AddOption("-Xverify:none", NULL);
1003 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1004 char *p = arg + 5;
1005 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1006 if (*p) {
1007 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1008 } else {
1009 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1010 }
1011 AddOption(tmp, NULL);
1012 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1013 JLI_StrCCmp(arg, "-oss") == 0 ||
1014 JLI_StrCCmp(arg, "-ms") == 0 ||
1015 JLI_StrCCmp(arg, "-mx") == 0) {
1016 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1017 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1018 AddOption(tmp, NULL);
1019 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1020 JLI_StrCmp(arg, "-cs") == 0 ||
1021 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1022 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001023 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001024 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1025 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1026 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1027 JLI_StrCCmp(arg, "-splash:") == 0) {
1028 ; /* Ignore machine independent options already handled */
1029 } else if (RemovableOption(arg) ) {
1030 ; /* Do not pass option to vm. */
1031 } else {
1032 AddOption(arg, NULL);
1033 }
1034 }
1035
1036 if (--argc >= 0) {
1037 if (jarflag) {
1038 *pjarfile = *argv++;
1039 *pclassname = 0;
1040 } else {
1041 *pjarfile = 0;
1042 *pclassname = *argv++;
1043 }
1044 *pargc = argc;
1045 *pargv = argv;
1046 }
1047
1048 return JNI_TRUE;
1049}
1050
1051/*
1052 * Initializes the Java Virtual Machine. Also frees options array when
1053 * finished.
1054 */
1055static jboolean
1056InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1057{
1058 JavaVMInitArgs args;
1059 jint r;
1060
1061 memset(&args, 0, sizeof(args));
1062 args.version = JNI_VERSION_1_2;
1063 args.nOptions = numOptions;
1064 args.options = options;
1065 args.ignoreUnrecognized = JNI_FALSE;
1066
1067 if (JLI_IsTraceLauncher()) {
1068 int i = 0;
1069 printf("JavaVM args:\n ");
1070 printf("version 0x%08lx, ", (long)args.version);
1071 printf("ignoreUnrecognized is %s, ",
1072 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1073 printf("nOptions is %ld\n", (long)args.nOptions);
1074 for (i = 0; i < numOptions; i++)
1075 printf(" option[%2d] = '%s'\n",
1076 i, args.options[i].optionString);
1077 }
1078
1079 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1080 JLI_MemFree(options);
1081 return r == JNI_OK;
1082}
1083
1084
1085#define NULL_CHECK0(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001086 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001087 return 0; \
1088 }
1089
1090#define NULL_CHECK(e) if ((e) == 0) { \
ksrini0e817162008-08-26 10:21:20 -07001091 JLI_ReportErrorMessage(JNI_ERROR); \
duke6e45e102007-12-01 00:00:00 +00001092 return; \
1093 }
1094
1095static jstring platformEncoding = NULL;
1096static jstring getPlatformEncoding(JNIEnv *env) {
1097 if (platformEncoding == NULL) {
1098 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
1099 if (propname) {
1100 jclass cls;
1101 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001102 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke6e45e102007-12-01 00:00:00 +00001103 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1104 env, cls,
1105 "getProperty",
1106 "(Ljava/lang/String;)Ljava/lang/String;"));
1107 platformEncoding = (*env)->CallStaticObjectMethod (
1108 env, cls, mid, propname);
1109 }
1110 }
1111 return platformEncoding;
1112}
1113
1114static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
1115 jclass cls;
1116 jmethodID mid;
ksrini20a64b22008-09-24 15:07:41 -07001117 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke6e45e102007-12-01 00:00:00 +00001118 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
1119 env, cls,
1120 "isSupported",
1121 "(Ljava/lang/String;)Z"));
1122 return (*env)->CallStaticBooleanMethod(env, cls, mid, enc);
1123}
1124
1125/*
1126 * Returns a new Java string object for the specified platform string.
1127 */
1128static jstring
1129NewPlatformString(JNIEnv *env, char *s)
1130{
1131 int len = (int)JLI_StrLen(s);
1132 jclass cls;
1133 jmethodID mid;
1134 jbyteArray ary;
1135 jstring enc;
1136
1137 if (s == NULL)
1138 return 0;
1139 enc = getPlatformEncoding(env);
1140
1141 ary = (*env)->NewByteArray(env, len);
1142 if (ary != 0) {
1143 jstring str = 0;
1144 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1145 if (!(*env)->ExceptionOccurred(env)) {
ksrini20a64b22008-09-24 15:07:41 -07001146 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001147 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke6e45e102007-12-01 00:00:00 +00001148 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1149 "([BLjava/lang/String;)V"));
1150 str = (*env)->NewObject(env, cls, mid, ary, enc);
1151 } else {
1152 /*If the encoding specified in sun.jnu.encoding is not
1153 endorsed by "Charset.isSupported" we have to fall back
1154 to use String(byte[]) explicitly here without specifying
1155 the encoding name, in which the StringCoding class will
1156 pickup the iso-8859-1 as the fallback converter for us.
1157 */
duke6e45e102007-12-01 00:00:00 +00001158 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
1159 "([B)V"));
1160 str = (*env)->NewObject(env, cls, mid, ary);
1161 }
1162 (*env)->DeleteLocalRef(env, ary);
1163 return str;
1164 }
1165 }
1166 return 0;
1167}
1168
1169/*
1170 * Returns a new array of Java string objects for the specified
1171 * array of platform strings.
1172 */
1173static jobjectArray
1174NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1175{
1176 jarray cls;
1177 jarray ary;
1178 int i;
1179
ksrini20a64b22008-09-24 15:07:41 -07001180 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001181 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1182 for (i = 0; i < strc; i++) {
1183 jstring str = NewPlatformString(env, *strv++);
1184 NULL_CHECK0(str);
1185 (*env)->SetObjectArrayElement(env, ary, i, str);
1186 (*env)->DeleteLocalRef(env, str);
1187 }
1188 return ary;
1189}
1190
1191/*
ksrini20a64b22008-09-24 15:07:41 -07001192 * Loads a class and verifies that the main class is present and it is ok to
1193 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001194 */
1195static jclass
ksrini20a64b22008-09-24 15:07:41 -07001196LoadMainClass(JNIEnv *env, jboolean isJar, char *name)
duke6e45e102007-12-01 00:00:00 +00001197{
duke6e45e102007-12-01 00:00:00 +00001198 jclass cls;
ksrini20a64b22008-09-24 15:07:41 -07001199 jmethodID mid;
1200 jstring str;
1201 jobject result;
duke6e45e102007-12-01 00:00:00 +00001202 jlong start, end;
1203
ksrini20a64b22008-09-24 15:07:41 -07001204 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001205 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001206 }
1207 NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
1208 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
1209 "(ZZLjava/lang/String;)Ljava/lang/Object;"));
1210 str = (*env)->NewStringUTF(env, name);
1211 result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, isJar, str);
duke6e45e102007-12-01 00:00:00 +00001212
1213 if (JLI_IsTraceLauncher()) {
1214 end = CounterGet();
1215 printf("%ld micro seconds to load main class\n",
1216 (long)(jint)Counter2Micros(end-start));
1217 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1218 }
1219
ksrini20a64b22008-09-24 15:07:41 -07001220 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001221}
1222
duke6e45e102007-12-01 00:00:00 +00001223/*
1224 * For tools, convert command line args thus:
1225 * javac -cp foo:foo/"*" -J-ms32m ...
1226 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1227 *
1228 * Takes 4 parameters, and returns the populated arguments
1229 */
1230static void
1231TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1232{
1233 int argc = *pargc;
1234 char **argv = *pargv;
1235 int nargc = argc + jargc;
1236 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1237 int i;
1238
1239 *pargc = nargc;
1240 *pargv = nargv;
1241
1242 /* Copy the VM arguments (i.e. prefixed with -J) */
1243 for (i = 0; i < jargc; i++) {
1244 const char *arg = jargv[i];
1245 if (arg[0] == '-' && arg[1] == 'J') {
1246 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1247 }
1248 }
1249
1250 for (i = 0; i < argc; i++) {
1251 char *arg = argv[i];
1252 if (arg[0] == '-' && arg[1] == 'J') {
1253 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001254 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001255 exit(1);
1256 }
1257 *nargv++ = arg + 2;
1258 }
1259 }
1260
1261 /* Copy the rest of the arguments */
1262 for (i = 0; i < jargc ; i++) {
1263 const char *arg = jargv[i];
1264 if (arg[0] != '-' || arg[1] != 'J') {
1265 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1266 }
1267 }
1268 for (i = 0; i < argc; i++) {
1269 char *arg = argv[i];
1270 if (arg[0] == '-') {
1271 if (arg[1] == 'J')
1272 continue;
1273 if (IsWildCardEnabled() && arg[1] == 'c'
1274 && (JLI_StrCmp(arg, "-cp") == 0 ||
1275 JLI_StrCmp(arg, "-classpath") == 0)
1276 && i < argc - 1) {
1277 *nargv++ = arg;
1278 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1279 i++;
1280 continue;
1281 }
1282 }
1283 *nargv++ = arg;
1284 }
1285 *nargv = 0;
1286}
1287
1288/*
1289 * For our tools, we try to add 3 VM options:
1290 * -Denv.class.path=<envcp>
1291 * -Dapplication.home=<apphome>
1292 * -Djava.class.path=<appcp>
1293 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1294 * tells javac where to find binary classes through this environment
1295 * variable. Notice that users will be able to compile against our
1296 * tools classes (sun.tools.javac.Main) only if they explicitly add
1297 * tools.jar to CLASSPATH.
1298 * <apphome> is the directory where the application is installed.
1299 * <appcp> is the classpath to where our apps' classfiles are.
1300 */
1301static jboolean
1302AddApplicationOptions(int cpathc, const char **cpathv)
1303{
1304 char *envcp, *appcp, *apphome;
1305 char home[MAXPATHLEN]; /* application home */
1306 char separator[] = { PATH_SEPARATOR, '\0' };
1307 int size, i;
1308
1309 {
1310 const char *s = getenv("CLASSPATH");
1311 if (s) {
1312 s = (char *) JLI_WildcardExpandClasspath(s);
1313 /* 40 for -Denv.class.path= */
1314 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1315 sprintf(envcp, "-Denv.class.path=%s", s);
1316 AddOption(envcp, NULL);
1317 }
1318 }
1319
1320 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001321 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001322 return JNI_FALSE;
1323 }
1324
1325 /* 40 for '-Dapplication.home=' */
1326 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1327 sprintf(apphome, "-Dapplication.home=%s", home);
1328 AddOption(apphome, NULL);
1329
1330 /* How big is the application's classpath? */
1331 size = 40; /* 40: "-Djava.class.path=" */
1332 for (i = 0; i < cpathc; i++) {
1333 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1334 }
1335 appcp = (char *)JLI_MemAlloc(size + 1);
1336 JLI_StrCpy(appcp, "-Djava.class.path=");
1337 for (i = 0; i < cpathc; i++) {
1338 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1339 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1340 JLI_StrCat(appcp, separator); /* ; */
1341 }
1342 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1343 AddOption(appcp, NULL);
1344 return JNI_TRUE;
1345}
1346
1347/*
1348 * inject the -Dsun.java.command pseudo property into the args structure
1349 * this pseudo property is used in the HotSpot VM to expose the
1350 * Java class name and arguments to the main method to the VM. The
1351 * HotSpot VM uses this pseudo property to store the Java class name
1352 * (or jar file name) and the arguments to the class's main method
1353 * to the instrumentation memory region. The sun.java.command pseudo
1354 * property is not exported by HotSpot to the Java layer.
1355 */
1356void
1357SetJavaCommandLineProp(char *classname, char *jarfile,
1358 int argc, char **argv)
1359{
1360
1361 int i = 0;
1362 size_t len = 0;
1363 char* javaCommand = NULL;
1364 char* dashDstr = "-Dsun.java.command=";
1365
1366 if (classname == NULL && jarfile == NULL) {
1367 /* unexpected, one of these should be set. just return without
1368 * setting the property
1369 */
1370 return;
1371 }
1372
1373 /* if the class name is not set, then use the jarfile name */
1374 if (classname == NULL) {
1375 classname = jarfile;
1376 }
1377
1378 /* determine the amount of memory to allocate assuming
1379 * the individual components will be space separated
1380 */
1381 len = JLI_StrLen(classname);
1382 for (i = 0; i < argc; i++) {
1383 len += JLI_StrLen(argv[i]) + 1;
1384 }
1385
1386 /* allocate the memory */
1387 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1388
1389 /* build the -D string */
1390 *javaCommand = '\0';
1391 JLI_StrCat(javaCommand, dashDstr);
1392 JLI_StrCat(javaCommand, classname);
1393
1394 for (i = 0; i < argc; i++) {
1395 /* the components of the string are space separated. In
1396 * the case of embedded white space, the relationship of
1397 * the white space separated components to their true
1398 * positional arguments will be ambiguous. This issue may
1399 * be addressed in a future release.
1400 */
1401 JLI_StrCat(javaCommand, " ");
1402 JLI_StrCat(javaCommand, argv[i]);
1403 }
1404
1405 AddOption(javaCommand, NULL);
1406}
1407
1408/*
1409 * JVM would like to know if it's created by a standard Sun launcher, or by
1410 * user native application, the following property indicates the former.
1411 */
1412void SetJavaLauncherProp() {
1413 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1414}
1415
1416/*
1417 * Prints the version information from the java.version and other properties.
1418 */
1419static void
1420PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1421{
1422 jclass ver;
1423 jmethodID print;
1424
ksrini20a64b22008-09-24 15:07:41 -07001425 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001426 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1427 ver,
1428 (extraLF == JNI_TRUE) ? "println" : "print",
1429 "()V"
1430 )
1431 );
1432
1433 (*env)->CallStaticVoidMethod(env, ver, print);
1434}
1435
1436/*
ksrini20a64b22008-09-24 15:07:41 -07001437 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001438 */
1439static void
1440PrintUsage(JNIEnv* env, jboolean doXUsage)
1441{
1442 jclass cls;
1443 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1444 jstring jprogname, vm1, vm2;
1445 int i;
1446
ksrini20a64b22008-09-24 15:07:41 -07001447 NULL_CHECK(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001448
1449
1450 if (doXUsage) {
1451 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1452 "printXUsageMessage", "(Z)V"));
1453 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, JNI_TRUE);
1454 } else {
1455 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1456 "initHelpMessage", "(Ljava/lang/String;)V"));
1457
1458 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1459 "(Ljava/lang/String;Ljava/lang/String;)V"));
1460
1461 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1462 "appendVmSynonymMessage",
1463 "(Ljava/lang/String;Ljava/lang/String;)V"));
1464 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1465 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1466
1467 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1468 "printHelpMessage", "(Z)V"));
1469
1470 jprogname = (*env)->NewStringUTF(env, _program_name);
1471
1472 /* Initialize the usage message with the usual preamble */
1473 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1474
1475
1476 /* Assemble the other variant part of the usage */
1477 if ((knownVMs[0].flag == VM_KNOWN) ||
1478 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1479 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1480 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1481 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1482 }
1483 for (i=1; i<knownVMsCount; i++) {
1484 if (knownVMs[i].flag == VM_KNOWN) {
1485 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1486 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1487 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1488 }
1489 }
1490 for (i=1; i<knownVMsCount; i++) {
1491 if (knownVMs[i].flag == VM_ALIASED_TO) {
1492 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1493 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1494 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1495 }
1496 }
1497
1498 /* The first known VM is the default */
1499 {
1500 jboolean isServerClassMachine = ServerClassMachine();
1501
1502 const char* defaultVM = knownVMs[0].name+1;
1503 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1504 defaultVM = knownVMs[0].server_class+1;
1505 }
1506
1507 vm1 = (*env)->NewStringUTF(env, defaultVM);
1508 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1509 }
1510
1511 /* Complete the usage message and print to stderr*/
1512 (*env)->CallStaticVoidMethod(env, cls, printHelp, JNI_TRUE);
1513 }
1514 return;
1515}
1516
1517/*
1518 * Read the jvm.cfg file and fill the knownJVMs[] array.
1519 *
1520 * The functionality of the jvm.cfg file is subject to change without
1521 * notice and the mechanism will be removed in the future.
1522 *
1523 * The lexical structure of the jvm.cfg file is as follows:
1524 *
1525 * jvmcfg := { vmLine }
1526 * vmLine := knownLine
1527 * | aliasLine
1528 * | warnLine
1529 * | ignoreLine
1530 * | errorLine
1531 * | predicateLine
1532 * | commentLine
1533 * knownLine := flag "KNOWN" EOL
1534 * warnLine := flag "WARN" EOL
1535 * ignoreLine := flag "IGNORE" EOL
1536 * errorLine := flag "ERROR" EOL
1537 * aliasLine := flag "ALIASED_TO" flag EOL
1538 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1539 * commentLine := "#" text EOL
1540 * flag := "-" identifier
1541 *
1542 * The semantics are that when someone specifies a flag on the command line:
1543 * - if the flag appears on a knownLine, then the identifier is used as
1544 * the name of the directory holding the JVM library (the name of the JVM).
1545 * - if the flag appears as the first flag on an aliasLine, the identifier
1546 * of the second flag is used as the name of the JVM.
1547 * - if the flag appears on a warnLine, the identifier is used as the
1548 * name of the JVM, but a warning is generated.
1549 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1550 * name of a JVM, but the identifier is ignored and the default vm used
1551 * - if the flag appears on an errorLine, an error is generated.
1552 * - if the flag appears as the first flag on a predicateLine, and
1553 * the machine on which you are running passes the predicate indicated,
1554 * then the identifier of the second flag is used as the name of the JVM,
1555 * otherwise the identifier of the first flag is used as the name of the JVM.
1556 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1557 * file determines the name of the JVM.
1558 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1559 * since they only make sense if someone hasn't specified the name of the
1560 * JVM on the command line.
1561 *
1562 * The intent of the jvm.cfg file is to allow several JVM libraries to
1563 * be installed in different subdirectories of a single JRE installation,
1564 * for space-savings and convenience in testing.
1565 * The intent is explicitly not to provide a full aliasing or predicate
1566 * mechanism.
1567 */
1568jint
1569ReadKnownVMs(const char *jrepath, const char * arch, jboolean speculative)
1570{
1571 FILE *jvmCfg;
1572 char jvmCfgName[MAXPATHLEN+20];
1573 char line[MAXPATHLEN+20];
1574 int cnt = 0;
1575 int lineno = 0;
1576 jlong start, end;
1577 int vmType;
1578 char *tmpPtr;
1579 char *altVMName = NULL;
1580 char *serverClassVMName = NULL;
1581 static char *whiteSpace = " \t";
1582 if (JLI_IsTraceLauncher()) {
1583 start = CounterGet();
1584 }
1585
1586 JLI_StrCpy(jvmCfgName, jrepath);
1587 JLI_StrCat(jvmCfgName, FILESEP "lib" FILESEP);
1588 JLI_StrCat(jvmCfgName, arch);
1589 JLI_StrCat(jvmCfgName, FILESEP "jvm.cfg");
1590
1591 jvmCfg = fopen(jvmCfgName, "r");
1592 if (jvmCfg == NULL) {
1593 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001594 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001595 exit(1);
1596 } else {
1597 return -1;
1598 }
1599 }
1600 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1601 vmType = VM_UNKNOWN;
1602 lineno++;
1603 if (line[0] == '#')
1604 continue;
1605 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001606 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001607 }
1608 if (cnt >= knownVMsLimit) {
1609 GrowKnownVMs(cnt);
1610 }
1611 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1612 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1613 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001614 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001615 } else {
1616 /* Null-terminate this string for JLI_StringDup below */
1617 *tmpPtr++ = 0;
1618 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1619 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001620 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001621 } else {
1622 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1623 vmType = VM_KNOWN;
1624 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1625 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1626 if (*tmpPtr != 0) {
1627 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1628 }
1629 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001630 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001631 } else {
1632 /* Null terminate altVMName */
1633 altVMName = tmpPtr;
1634 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1635 *tmpPtr = 0;
1636 vmType = VM_ALIASED_TO;
1637 }
1638 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1639 vmType = VM_WARN;
1640 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1641 vmType = VM_IGNORE;
1642 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1643 vmType = VM_ERROR;
1644 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1645 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1646 if (*tmpPtr != 0) {
1647 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1648 }
1649 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001650 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001651 } else {
1652 /* Null terminate server class VM name */
1653 serverClassVMName = tmpPtr;
1654 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1655 *tmpPtr = 0;
1656 vmType = VM_IF_SERVER_CLASS;
1657 }
1658 } else {
ksrini0e817162008-08-26 10:21:20 -07001659 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001660 vmType = VM_KNOWN;
1661 }
1662 }
1663 }
1664
1665 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1666 if (vmType != VM_UNKNOWN) {
1667 knownVMs[cnt].name = JLI_StringDup(line);
1668 knownVMs[cnt].flag = vmType;
1669 switch (vmType) {
1670 default:
1671 break;
1672 case VM_ALIASED_TO:
1673 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1674 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1675 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1676 break;
1677 case VM_IF_SERVER_CLASS:
1678 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1679 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1680 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1681 break;
1682 }
1683 cnt++;
1684 }
1685 }
1686 fclose(jvmCfg);
1687 knownVMsCount = cnt;
1688
1689 if (JLI_IsTraceLauncher()) {
1690 end = CounterGet();
1691 printf("%ld micro seconds to parse jvm.cfg\n",
1692 (long)(jint)Counter2Micros(end-start));
1693 }
1694
1695 return cnt;
1696}
1697
1698
1699static void
1700GrowKnownVMs(int minimum)
1701{
1702 struct vmdesc* newKnownVMs;
1703 int newMax;
1704
1705 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1706 if (newMax <= minimum) {
1707 newMax = minimum;
1708 }
1709 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1710 if (knownVMs != NULL) {
1711 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1712 }
1713 JLI_MemFree(knownVMs);
1714 knownVMs = newKnownVMs;
1715 knownVMsLimit = newMax;
1716}
1717
1718
1719/* Returns index of VM or -1 if not found */
1720static int
1721KnownVMIndex(const char* name)
1722{
1723 int i;
1724 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1725 for (i = 0; i < knownVMsCount; i++) {
1726 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1727 return i;
1728 }
1729 }
1730 return -1;
1731}
1732
1733static void
1734FreeKnownVMs()
1735{
1736 int i;
1737 for (i = 0; i < knownVMsCount; i++) {
1738 JLI_MemFree(knownVMs[i].name);
1739 knownVMs[i].name = NULL;
1740 }
1741 JLI_MemFree(knownVMs);
1742}
1743
1744
1745/*
1746 * Displays the splash screen according to the jar file name
1747 * and image file names stored in environment variables
1748 */
1749static void
1750ShowSplashScreen()
1751{
1752 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1753 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1754 int data_size;
1755 void *image_data;
1756 if (jar_name) {
1757 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1758 if (image_data) {
1759 DoSplashInit();
1760 DoSplashLoadMemory(image_data, data_size);
1761 JLI_MemFree(image_data);
1762 }
1763 } else if (file_name) {
1764 DoSplashInit();
1765 DoSplashLoadFile(file_name);
1766 } else {
1767 return;
1768 }
1769 DoSplashSetFileJarName(file_name, jar_name);
1770
1771 /*
1772 * Done with all command line processing and potential re-execs so
1773 * clean up the environment.
1774 */
1775 (void)UnsetEnv(ENV_ENTRY);
1776 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1777 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1778
1779 JLI_MemFree(splash_jar_entry);
1780 JLI_MemFree(splash_file_entry);
1781
1782}
1783
1784const char*
1785GetDotVersion()
1786{
1787 return _dVersion;
1788}
1789
1790const char*
1791GetFullVersion()
1792{
1793 return _fVersion;
1794}
1795
1796const char*
1797GetProgramName()
1798{
1799 return _program_name;
1800}
1801
1802const char*
1803GetLauncherName()
1804{
1805 return _launcher_name;
1806}
1807
1808jint
1809GetErgoPolicy()
1810{
1811 return _ergo_policy;
1812}
1813
1814jboolean
1815IsJavaArgs()
1816{
1817 return _is_java_args;
1818}
1819
1820static jboolean
1821IsWildCardEnabled()
1822{
1823 return _wc_enabled;
1824}
1825
1826static int
1827ContinueInNewThread(InvocationFunctions* ifn, int argc,
1828 char **argv, char *jarfile, char *classname, int ret)
1829{
1830
1831 /*
1832 * If user doesn't specify stack size, check if VM has a preference.
1833 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1834 * return its default stack size through the init args structure.
1835 */
1836 if (threadStackSize == 0) {
1837 struct JDK1_1InitArgs args1_1;
1838 memset((void*)&args1_1, 0, sizeof(args1_1));
1839 args1_1.version = JNI_VERSION_1_1;
1840 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1841 if (args1_1.javaStackSize > 0) {
1842 threadStackSize = args1_1.javaStackSize;
1843 }
1844 }
1845
1846 { /* Create a new thread to create JVM and invoke main method */
1847 JavaMainArgs args;
1848 int rslt;
1849
1850 args.argc = argc;
1851 args.argv = argv;
1852 args.jarfile = jarfile;
1853 args.classname = classname;
1854 args.ifn = *ifn;
1855
1856 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1857 /* If the caller has deemed there is an error we
1858 * simply return that, otherwise we return the value of
1859 * the callee
1860 */
1861 return (ret != 0) ? ret : rslt;
1862 }
1863}
1864
1865static void
1866DumpState()
1867{
1868 if (!JLI_IsTraceLauncher()) return ;
1869 printf("Launcher state:\n");
1870 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1871 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1872 printf("\tprogram name:%s\n", GetProgramName());
1873 printf("\tlauncher name:%s\n", GetLauncherName());
1874 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1875 printf("\tfullversion:%s\n", GetFullVersion());
1876 printf("\tdotversion:%s\n", GetDotVersion());
1877 printf("\tergo_policy:");
1878 switch(GetErgoPolicy()) {
1879 case NEVER_SERVER_CLASS:
1880 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1881 break;
1882 case ALWAYS_SERVER_CLASS:
1883 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1884 break;
1885 default:
1886 printf("DEFAULT_ERGONOMICS_POLICY\n");
1887 }
1888}
1889
1890/*
1891 * Return JNI_TRUE for an option string that has no effect but should
1892 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1893 * Solaris SPARC, this screening needs to be done if:
1894 * 1) LD_LIBRARY_PATH does _not_ need to be reset and
1895 * 2) -d32 or -d64 is passed to a binary with a matching data model
1896 * (the exec in SetLibraryPath removes -d<n> options and points the
1897 * exec to the proper binary). When this exec is not done, these options
1898 * would end up getting passed onto the vm.
1899 */
1900jboolean
1901RemovableOption(char * option)
1902{
1903 /*
1904 * Unconditionally remove both -d32 and -d64 options since only
1905 * the last such options has an effect; e.g.
1906 * java -d32 -d64 -d32 -version
1907 * is equivalent to
1908 * java -d32 -version
1909 */
1910
1911 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1912 (JLI_StrCCmp(option, "-d64") == 0 ) )
1913 return JNI_TRUE;
1914 else
1915 return JNI_FALSE;
1916}
1917
1918/*
1919 * A utility procedure to always print to stderr
1920 */
1921void
ksrini0e817162008-08-26 10:21:20 -07001922JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001923{
1924 va_list vl;
1925 va_start(vl, fmt);
1926 vfprintf(stderr, fmt, vl);
1927 fprintf(stderr, "\n");
1928 va_end(vl);
1929}