annotate src/os/bsd/launcher/java.c @ 511:897a4fb539b6

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