annotate src/os/solaris/launcher/java.c @ 470:ad8c8ca4ab0f

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