java.c revision 1934
1892N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 1892N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1892N/A * This code is free software; you can redistribute it and/or modify it 1892N/A * under the terms of the GNU General Public License version 2 only, as 1892N/A * published by the Free Software Foundation. 1892N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1892N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1892N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1892N/A * version 2 for more details (a copy is included in the LICENSE file that 1892N/A * You should have received a copy of the GNU General Public License version 1892N/A * 2 along with this work; if not, write to the Free Software Foundation, 1892N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1892N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1892N/A * Gamma (Hotspot internal engineering test) launcher based on 6.0u22 JDK, 1892N/A * search "GAMMA" for gamma specific changes. 1892N/A * GAMMA: gamma launcher is much simpler than regular java launcher in that 1892N/A * JVM is either statically linked in or it is installed in the 1892N/A * same directory where the launcher exists, so we don't have to 1892N/A * worry about choosing the right JVM based on command line flag, jar 1892N/A * file and/or ergonomics. Intead of removing unused logic from source 1892N/A * they are commented out with #ifndef GAMMA, hopefully it'll be easier 1892N/A * to maintain this file in sync with regular JDK launcher. 1892N/A * Shared source for 'java' command line tool. 1892N/A * If JAVA_ARGS is defined, then acts as a launcher for applications. For 1892N/A * instance, the JDK command line tools such as javac and javadoc (see 1892N/A * makefiles for more details) are built with this program. Any arguments 1892N/A * prefixed with '-J' will be passed directly to the 'java' command. 1892N/A * One job of the launcher is to remove command line options which the 1892N/A * vm does not understand and will not process. These options include 1892N/A * options which select which style of vm is run (e.g. -client and 1892N/A * -server) as well as options which select the data model to use. 1892N/A * Additionally, for tools which invoke an underlying vm "-J-foo" 1892N/A * options are turned into "-foo" options to the vm. This option 1892N/A * filtering is handled in a number of places in the launcher, some of 1892N/A * it in machine-dependent code. In this file, the function 1892N/A * CheckJVMType removes vm style options and TranslateApplicationArgs 1892N/A * removes "-J" prefixes. On unix platforms, the 1892N/A * processes and removes -d<n> options. However, in case 1892N/A * CreateExecutionEnvironment does not need to exec because 1892N/A * LD_LIBRARY_PATH is set acceptably and the data model does not need 1892N/A * to be changed, ParseArguments will screen out the redundant -d<n> 1892N/A * options and prevent them from being passed to the vm; this is done 1892N/A * by using the machine-dependent call 1892N/A * RemovableMachineDependentOption. 1892N/A * The following environment variable is used to influence the behavior 1892N/A * of the jre exec'd through the SelectVersion routine. The command line 1892N/A * options which specify the version are not passed to the exec'd version, 1892N/A * because that jre may be an older version which wouldn't recognize them. 1892N/A * This environment variable is known to this (and later) version and serves 1892N/A * to suppress the version selection code. This is not only for efficiency, 1892N/A * but also for correctness, since any command line options have been 1892N/A * removed which would cause any value found in the manifest to be used. 1892N/A * This would be incorrect because the command line options are defined 1892N/A * The value associated with this environment variable is the MainClass 1892N/A * name from within the executable jar file (if any). This is strictly a 1892N/A * performance enhancement to avoid re-reading the jar file manifest. 1892N/A * A NOTE TO DEVELOPERS: For performance reasons it is important that 1892N/A * the program image remain relatively small until after SelectVersion 1892N/A * CreateExecutionEnvironment have finished their possibly recursive 1892N/A * processing. Watch everything, but resist all temptations to use Java 1892N/A * Entries for splash screen environment variables. 1892N/A * putenv is performed in SelectVersion. We need 1892N/A * them in memory until UnsetEnv, so they are made static 1892N/A * global instead of auto local. 1892N/A * List of VM options to be specified when the VM is created. 1892N/A * Prototypes for functions internal to launcher. 1892N/A/* Values for vmdesc.flag */ 1892N/A/* flag which if set suppresses error messages from the launcher */ 1892N/A * Running Java code in primordial thread caused many problems. We will 1892N/A * create a new thread to invoke JVM. See 6316197 for more information. 1892N/A * Make sure the specified version of the JRE is running. 1892N/A * There are three things to note about the SelectVersion() routine: 1892N/A * 1) If the version running isn't correct, this routine doesn't 1892N/A * return (either the correct version has been exec'd or an error 1892N/A * 2) Argc and Argv in this scope are *not* altered by this routine. 1892N/A * It is the responsibility of subsequent code to ignore the 1892N/A * arguments handled by this routine. 1892N/A * 3) As a side-effect, the variable "main_class" is guaranteed to 1892N/A * be set (if it should ever be set). This isn't exactly the 1892N/A * poster child for structured programming, but it is a small 1892N/A * price to pay for not processing a jar file operand twice. 1892N/A * (Note: This side effect has been disabled. See comment on 1892N/A#
else /* java, oldjava, javaw and friends */ 1892N/A /* Preprocess wrapper arguments */ 1892N/A /* Set default CLASSPATH */ 1892N/A * Parse command line options; if the return value of 1892N/A * ParseArguments is false, the program should exit. 1892N/A /* Override class path if -jar flag was specified */ 1892N/A /* Show the splash screen if needed */ 1892N/A * Done with all command line processing and potential re-execs so 1892N/A * clean up the environment. 1892N/A * If user doesn't specify stack size, check if VM has a preference. 1892N/A * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will 1892N/A * return its default stack size through the init args structure. 1892N/A {
/* Create a new thread to create JVM and invoke main method */ 1892N/A * Error message to print or display; by default the message will 1892N/A * only be displayed in a window. 1892N/A char *
message =
"Fatal exception occurred. Program will exit.";
1892N/A /* Initialize the virtual machine */ 1892N/A /* If the user specified neither a class name nor a JAR file */ 1892N/A * Get the application's main class. 1892N/A * See bugid 5030265. The Main-Class name has already been parsed 1892N/A * from the manifest, but not parsed properly for UTF-8 support. 1892N/A * Hence the code here ignores the value previously extracted and 1892N/A * uses the pre-existing code to reextract the value. This is 1892N/A * possibly an end of release cycle expedient. However, it has 1892N/A * also been discovered that passing some character sets through 1892N/A * the environment has "strange" behavior on some variants of 1892N/A * Windows. Hence, maybe the manifest parsing code local to the 1892N/A * launcher should never be enhanced. 1892N/A * Hence, future work should either: 1892N/A * 1) Correct the local parsing code and verify that the 1892N/A * Main-Class attribute gets properly passed through 1892N/A * 2) Remove the vestages of maintaining main_class through 1892N/A * the environment (and remove these comments). 1892N/A const char *
format =
"Failed to load Main-Class manifest " 1892N/A const char *
format =
"Could not find the main class: %s. Program will exit.";
1892N/A const char *
format =
"Could not find the main class: %s. Program will exit.";
1892N/A /* Get the application's main method */ 1892N/A {
/* Make sure the main method is public */ 1892N/A if ((
mods &
1) == 0) {
/* if (!Modifier.isPublic(mods)) ... */ 1892N/A * The launcher's exit code (in the absence of calls to 1892N/A * System.exit) will be non-zero if main threw an exception. 1892N/A * Detach the main thread so that it appears to have ended when 1892N/A * the application's main method exits. This will invoke the 1892N/A * uncaught exception handler machinery if main threw an 1892N/A * exception. An uncaught exception handler cannot change the 1892N/A * launcher's return code except by calling System.exit. 1892N/A * Wait for all non-daemon threads to end, then destroy the VM. 1892N/A * This will actually create a trivial new Java waiter thread 1892N/A * named "DestroyJavaVM", but this will be seen as a different 1892N/A * thread from the one that executed main, even though they are 1892N/A * the same C thread. This allows mainThread.join() and 1892N/A * mainThread.isAlive() to work as expected. 1892N/A * Checks the command line options to find which JVM type was 1892N/A * specified. If no command line option was given for the JVM type, 1892N/A * the default type is used. The environment variable 1892N/A * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also 1892N/A * checked as ways of specifying which JVM type to invoke. 1892N/A /* To make things simpler we always copy the argv array */ 1892N/A /* The program name is always present */ 1892N/A /* Did the user pass an explicit VM type? */ 1892N/A /* Did the user specify an "alternate" VM? */ 1892N/A * Finish copying the arguments if we aborted the above loop. 1892N/A * NOTE that if we aborted via "break" then we did NOT copy the 1892N/A * last argument above, and in addition argi will be less than 1892N/A /* argv is null-terminated */ 1892N/A /* use the default VM type if not specified (no alias processing) */ 1892N/A /* Use a different VM type if we are on a server class machine? */ 1892N/A /* if using an alternate VM, no alias processing */ 1892N/A /* Resolve aliases first */ 1892N/A/* copied from HotSpot function "atomll()" */ 1892N/A while (*s !=
'\0' && *s >=
'0' && *s <=
'9') {
1892N/A // 4705540: illegal if more characters are found after the first non-digit 1892N/A /* Create JVM with default stack and let VM handle malformed -Xss string*/ 1892N/A * Adds a new VM option with the given given name and value. 1892N/A * Expand options array if needed to accommodate at least one more 1892N/A * The SelectVersion() routine ensures that an appropriate version of 1892N/A * the JRE is running. The specification for the appropriate version 1892N/A * is obtained from either the manifest of a jar file (preferred) or 1892N/A * from command line options. 1892N/A * The routine also parses splash screen command line options and 1892N/A * passes on their values in private environment variables. 1892N/A * If the version has already been selected, set *main_class 1892N/A * with the value passed through the environment (if any) and 1892N/A * Scan through the arguments for options relevant to multiple JRE 1892N/A * support. For reference, the command line syntax is defined as: 1892N/A * java [options] class [argument...] 1892N/A * java [options] -jar file.jar [argument...] 1892N/A * As the scan is performed, make a copy of the argument list with 1892N/A * the version specification options (new to 1.5) removed, so that 1892N/A * a version less than 1.5 can be exec'd. 1892N/A * Note that due to the syntax of the native Windows interface 1892N/A * CreateProcess(), processing similar to the following exists in 1892N/A * Changes here should be reproduced there. 1892N/A /* deal with "unfortunate" classpath syntax */ 1892N/A * Checking for headless toolkit option in the some way as AWT does: 1892N/A * "true" means true and any other value means false 1892N/A if (
argc <= 0) {
/* No operand? Possibly legit with -[full]version */ 1892N/A while (
argc-- > 0)
/* Copy over [argument...] */ 1892N/A * If there is a jar file, read the manifest. If the jarfile can't be 1892N/A * read, the manifest can't be read from the jar file, or the manifest 1892N/A * is corrupt, issue the appropriate error messages and exit. 1892N/A * Even if there isn't a jar file, construct a manifest_info structure 1892N/A * containing the command line information. It's a convenient way to carry 1892N/A * Command line splash screen option should have precedence 1892N/A * over the manifest, so the manifest data is used only if 1892N/A * splash_file_name has not been initialized above during command 1892N/A * Passing on splash screen info in environment variables 1892N/A * The JRE-Version and JRE-Restrict-Search values (if any) from the 1892N/A * manifest are overwritten by any specified on the command line. 1892N/A * "Valid" returns (other than unrecoverable errors) follow. Set 1892N/A * main_class as a side-effect of this routine. 1892N/A * If no version selection information is found either on the command 1892N/A * line or in the manifest, simply return. 1892N/A * Check for correct syntax of the version specification (JSR 56). 1892N/A * Find the appropriate JVM on the system. Just to be as forgiving as 1892N/A * possible, if the standard algorithms don't locate an appropriate 1892N/A * jre, check to see if the one running will satisfy the requirements. 1892N/A * This can happen on systems which haven't been set-up for multiple 1892N/A printf(
"JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
1892N/A "Unable to locate JRE meeting specification \"%s\"",
1892N/A * If I'm not the chosen one, exec the chosen one. Returning from 1892N/A * ExecJRE indicates that I am indeed the chosen one. 1892N/A * The private environment variable _JAVA_VERSION_SET is used to 1892N/A * prevent the chosen one from re-reading the manifest file and 1892N/A * using the values found within to override the (potential) command 1892N/A * line flags stripped from argv (because the target may not 1892N/A * understand them). Passing the MainClass value is an optimization 1892N/A * to avoid locating, expanding and parsing the manifest extra 1892N/A * Parses command line arguments. Returns JNI_FALSE if launcher 1892N/A * should exit without starting vm (e.g. certain version and usage 1892N/A * options); returns JNI_TRUE if vm needs to be started to process 1892N/A * given options. *pret (the launcher process return value) is set to 1892N/A * The following case provide backward compatibility with old-style 1892N/A "Warning: %s option is no longer supported.\n",
1892N/A ;
/* Ignore machine independent options already handled */ 1892N/A ;
/* Do not pass option to vm. */ 1892N/A * Initializes the Java Virtual Machine. Also frees options array when 1892N/A * Returns a new Java string object for the specified platform string. 1892N/A /*If the encoding specified in sun.jnu.encoding is not 1892N/A endorsed by "Charset.isSupported" we have to fall back 1892N/A to use String(byte[]) explicitly here without specifying 1892N/A the encoding name, in which the StringCoding class will 1892N/A pickup the iso-8859-1 as the fallback converter for us. 1892N/A * Returns a new array of Java string objects for the specified 1892N/A * array of platform strings. 1892N/A * Loads a class, convert the '.' to '/'. 1892N/A *s++ = (c ==
'.') ?
'/' : c;
1892N/A * Returns the main class name for the specified jar file. 1892N/A * For tools, convert command line args thus: 1892N/A * javac -cp foo:foo/"*" -J-ms32m ... 1892N/A * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ... 1892N/A /* Copy the VM arguments (i.e. prefixed with -J) */ 1892N/A /* Copy the rest of the arguments */ 1892N/A * For our tools, we try to add 3 VM options: 1892N/A * <envcp> is the user's setting of CLASSPATH -- for instance the user 1892N/A * tells javac where to find binary classes through this environment 1892N/A * variable. Notice that users will be able to compile against our 1892N/A * <apphome> is the directory where the application is installed. 1892N/A * <appcp> is the classpath to where our apps' classfiles are. 1892N/A /* How big is the application's classpath? */ 1892N/A * this pseudo property is used in the HotSpot VM to expose the 1892N/A * Java class name and arguments to the main method to the VM. The 1892N/A * HotSpot VM uses this pseudo property to store the Java class name 1892N/A * (or jar file name) and the arguments to the class's main method 1892N/A * property is not exported by HotSpot to the Java layer. 1892N/A /* unexpected, one of these should be set. just return without 1892N/A /* if the class name is not set, then use the jarfile name */ 1892N/A /* determine the amount of memory to allocate assuming 1892N/A * the individual components will be space separated 1892N/A /* the components of the string are space separated. In 1892N/A * the case of embedded white space, the relationship of 1892N/A * the white space separated components to their true 1892N/A * positional arguments will be ambiguous. This issue may 1892N/A * be addressed in a future release. 1892N/A * JVM would like to know if it's created by a standard Sun launcher, or by 1892N/A * user native application, the following property indicates the former. 1892N/A * Prints the version information from the java.version and other properties. 1892N/A * Prints default usage message. 1892N/A "Usage: %s [-options] class [args...]\n" 1892N/A " or %s [-options] -jar jarfile [args...]\n" 1892N/A " (to execute a jar file)\n" 1892N/A "where options include:\n",
1892N/A "the \"%s\" VM [deprecated]\n",
1892N/A /* The first known VM is the default */ 1892N/A reason =
"because you are running on a server-class machine.\n";
1892N/A" -cp <class search path of directories and zip/jar files>\n" 1892N/A" -classpath <class search path of directories and zip/jar files>\n" 1892N/A" A %c separated list of directories, JAR archives,\n" 1892N/A" and ZIP archives to search for class files.\n" 1892N/A" -verbose[:class|gc|jni]\n" 1892N/A" -version print product version and exit\n" 1892N/A" require the specified version to run\n" 1892N/A" -showversion print product version and continue\n" 1892N/A" -jre-restrict-search | -jre-no-restrict-search\n" 1892N/A" -? -help print this help message\n" 1892N/A" -X print help on non-standard options\n" 1892N/A" -ea[:<packagename>...|:<classname>]\n" 1892N/A" -enableassertions[:<packagename>...|:<classname>]\n" 1892N/A" -da[:<packagename>...|:<classname>]\n" 1892N/A" -disableassertions[:<packagename>...|:<classname>]\n" 1892N/A" -esa | -enablesystemassertions\n" 1892N/A" enable system assertions\n" 1892N/A" -dsa | -disablesystemassertions\n" 1892N/A" disable system assertions\n" 1892N/A" -agentlib:<libname>[=<options>]\n" 1892N/A" load native agent library <libname>, e.g. -agentlib:hprof\n" 1892N/A" see also, -agentlib:jdwp=help and -agentlib:hprof=help\n" 1892N/A" -agentpath:<pathname>[=<options>]\n" 1892N/A" load native agent library by full pathname\n" 1892N/A" -javaagent:<jarpath>[=<options>]\n" 1892N/A" load Java programming language agent, see java.lang.instrument\n" 1892N/A" show splash screen with specified image\n" 1892N/A * Print usage message for -X options. 1892N/A A 32 bit cushion to prevent buffer overrun, noting that 1892N/A fopen(3C) may fail if the buffer exceeds MAXPATHLEN. 1892N/A /* Note the FILE_SEPARATOR is platform dependent */ 1892N/A * Read the jvm.cfg file and fill the knownJVMs[] array. 1892N/A * The functionality of the jvm.cfg file is subject to change without 1892N/A * notice and the mechanism will be removed in the future. 1892N/A * The lexical structure of the jvm.cfg file is as follows: 1892N/A * knownLine := flag "KNOWN" EOL 1892N/A * warnLine := flag "WARN" EOL 1892N/A * ignoreLine := flag "IGNORE" EOL 1892N/A * errorLine := flag "ERROR" EOL 1892N/A * aliasLine := flag "ALIASED_TO" flag EOL 1892N/A * predicateLine := flag "IF_SERVER_CLASS" flag EOL 1892N/A * commentLine := "#" text EOL 1892N/A * The semantics are that when someone specifies a flag on the command line: 1892N/A * - if the flag appears on a knownLine, then the identifier is used as 1892N/A * the name of the directory holding the JVM library (the name of the JVM). 1892N/A * - if the flag appears as the first flag on an aliasLine, the identifier 1892N/A * of the second flag is used as the name of the JVM. 1892N/A * - if the flag appears on a warnLine, the identifier is used as the 1892N/A * name of the JVM, but a warning is generated. 1892N/A * - if the flag appears on an ignoreLine, the identifier is recognized as the 1892N/A * name of a JVM, but the identifier is ignored and the default vm used 1892N/A * - if the flag appears on an errorLine, an error is generated. 1892N/A * - if the flag appears as the first flag on a predicateLine, and 1892N/A * the machine on which you are running passes the predicate indicated, 1892N/A * then the identifier of the second flag is used as the name of the JVM, 1892N/A * otherwise the identifier of the first flag is used as the name of the JVM. 1892N/A * If no flag is given on the command line, the first vmLine of the jvm.cfg 1892N/A * file determines the name of the JVM. 1892N/A * PredicateLines are only interpreted on first vmLine of a jvm.cfg file, 1892N/A * since they only make sense if someone hasn't specified the name of the 1892N/A * The intent of the jvm.cfg file is to allow several JVM libraries to 1892N/A * be installed in different subdirectories of a single JRE installation, 1892N/A * for space-savings and convenience in testing. 1892N/A * The intent is explicitly not to provide a full aliasing or predicate 1892N/A /* Null-terminate this string for JLI_StringDup below */ 1892N/A /* Null terminate altVMName */ 1892N/A /* Null terminate server class VM name */ 1892N/A/* Returns index of VM or -1 if not found */ 1892N/A * Displays the splash screen according to the jar file name 1892N/A * and image file names stored in environment variables