java.c revision 3487
4632N/A * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4632N/A * This code is free software; you can redistribute it and/or modify it 4632N/A * under the terms of the GNU General Public License version 2 only, as 4632N/A * published by the Free Software Foundation. Oracle designates this 4632N/A * particular file as subject to the "Classpath" exception as provided 4632N/A * by Oracle in the LICENSE file that accompanied this code. 4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT 4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4632N/A * version 2 for more details (a copy is included in the LICENSE file that 4632N/A * You should have received a copy of the GNU General Public License version 4632N/A * 2 along with this work; if not, write to the Free Software Foundation, 4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4632N/A * Shared source for 'java' command line tool. 4632N/A * If JAVA_ARGS is defined, then acts as a launcher for applications. For 4632N/A * instance, the JDK command line tools such as javac and javadoc (see 4632N/A * makefiles for more details) are built with this program. Any arguments 4632N/A * prefixed with '-J' will be passed directly to the 'java' command. 4632N/A * One job of the launcher is to remove command line options which the 4632N/A * vm does not understand and will not process. These options include 4632N/A * options which select which style of vm is run (e.g. -client and 4632N/A * -server) as well as options which select the data model to use. 4632N/A * Additionally, for tools which invoke an underlying vm "-J-foo" 4632N/A * options are turned into "-foo" options to the vm. This option 4632N/A * filtering is handled in a number of places in the launcher, some of 4632N/A * it in machine-dependent code. In this file, the function 4632N/A * CheckJvmType removes vm style options and TranslateApplicationArgs 4632N/A * removes "-J" prefixes. The CreateExecutionEnvironment function processes 4632N/A * and removes -d<n> options. On unix, there is a possibility that the running 4632N/A * data model may not match to the desired data model, in this case an exec is 4632N/A * required to start the desired model. If the data models match, then 4632N/A * ParseArguments will remove the -d<n> flags. If the data models do not match 4632N/A * the CreateExecutionEnviroment will remove the -d<n> flags. 4632N/A * A NOTE TO DEVELOPERS: For performance reasons it is important that 4632N/A * the program image remain relatively small until after SelectVersion 4632N/A * CreateExecutionEnvironment have finished their possibly recursive 4632N/A * processing. Watch everything, but resist all temptations to use Java 4632N/A * Entries for splash screen environment variables. 4632N/A * putenv is performed in SelectVersion. We need 4632N/A * them in memory until UnsetEnv, so they are made static 4632N/A * global instead of auto local. 4632N/A * List of VM options to be specified when the VM is created. 4632N/A * Prototypes for functions internal to launcher. 4632N/A/* Values for vmdesc.flag */ 4632N/A * Running Java code in primordial thread caused many problems. We will 4632N/A * create a new thread to invoke JVM. See 6316197 for more information. 4632N/A = {
"Unknown",
"Main class",
"JAR file" };
4632N/A * Make sure the specified version of the JRE is running. 4632N/A * There are three things to note about the SelectVersion() routine: 4632N/A * 1) If the version running isn't correct, this routine doesn't 4632N/A * return (either the correct version has been exec'd or an error 4632N/A * 2) Argc and Argv in this scope are *not* altered by this routine. 4632N/A * It is the responsibility of subsequent code to ignore the 4632N/A * arguments handled by this routine. 4632N/A * 3) As a side-effect, the variable "main_class" is guaranteed to 4632N/A * be set (if it should ever be set). This isn't exactly the 4632N/A * poster child for structured programming, but it is a small 4632N/A * price to pay for not processing a jar file operand twice. 4632N/A * (Note: This side effect has been disabled. See comment on 4632N/A /* Preprocess wrapper arguments */ 4632N/A /* Set default CLASSPATH */ 4632N/A /* Parse command line options; if the return value of 4632N/A * ParseArguments is false, the program should exit. 4632N/A /* Override class path if -jar flag was specified */ 4632N/A /* Show the splash screen if needed */ 4632N/A * Always detach the main thread so that it appears to have ended when 4632N/A * the application's main method exits. This will invoke the 4632N/A * uncaught exception handler machinery if main threw an 4632N/A * exception. An uncaught exception handler cannot change the 4632N/A * launcher's return code except by calling System.exit. 4632N/A * Wait for all non-daemon threads to end, then destroy the VM. 4632N/A * This will actually create a trivial new Java waiter thread 4632N/A * named "DestroyJavaVM", but this will be seen as a different 4632N/A * thread from the one that executed main, even though they are 4632N/A * the same C thread. This allows mainThread.join() and 4632N/A * mainThread.isAlive() to work as expected. 4632N/A /* Initialize the virtual machine */ 4632N/A /* If the user specified neither a class name nor a JAR file */ 4632N/A * Get the application's main class. 4632N/A * See bugid 5030265. The Main-Class name has already been parsed 4632N/A * from the manifest, but not parsed properly for UTF-8 support. 4632N/A * Hence the code here ignores the value previously extracted and 4632N/A * uses the pre-existing code to reextract the value. This is 4632N/A * possibly an end of release cycle expedient. However, it has 4632N/A * also been discovered that passing some character sets through 4632N/A * the environment has "strange" behavior on some variants of 4632N/A * Windows. Hence, maybe the manifest parsing code local to the 4632N/A * launcher should never be enhanced. 4632N/A * Hence, future work should either: 4632N/A * 1) Correct the local parsing code and verify that the 4632N/A * Main-Class attribute gets properly passed through 4632N/A * 2) Remove the vestages of maintaining main_class through 4632N/A * the environment (and remove these comments). 4632N/A * The LoadMainClass not only loads the main class, it will also ensure 4632N/A * that the main method's signature is correct, therefore further checking 4632N/A * is not required. The main method is invoked here so that extraneous java 4632N/A * stacks are not in the application stack trace. 4632N/A * The launcher's exit code (in the absence of calls to 4632N/A * System.exit) will be non-zero if main threw an exception. 4632N/A * Checks the command line options to find which JVM type was 4632N/A * specified. If no command line option was given for the JVM type, 4632N/A * the default type is used. The environment variable 4632N/A * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also 4632N/A * checked as ways of specifying which JVM type to invoke. 4632N/A /* To make things simpler we always copy the argv array */ 4632N/A /* The program name is always present */ 4632N/A /* Did the user pass an explicit VM type? */ 4632N/A /* Did the user specify an "alternate" VM? */ 4632N/A * Finish copying the arguments if we aborted the above loop. 4632N/A * NOTE that if we aborted via "break" then we did NOT copy the 4632N/A * last argument above, and in addition argi will be less than 4632N/A /* argv is null-terminated */ 4632N/A /* use the default VM type if not specified (no alias processing) */ 4632N/A /* Use a different VM type if we are on a server class machine? */ 4632N/A /* if using an alternate VM, no alias processing */ 4632N/A /* Resolve aliases first */ 4632N/A/* copied from HotSpot function "atomll()" */ 4632N/A while (*s !=
'\0' && *s >=
'0' && *s <=
'9') {
4632N/A // 4705540: illegal if more characters are found after the first non-digit 4632N/A /* Create JVM with default stack and let VM handle malformed -Xss string*/ 4632N/A * Adds a new VM option with the given given name and value. 4632N/A * Expand options array if needed to accommodate at least one more 4632N/A * The SelectVersion() routine ensures that an appropriate version of 4632N/A * the JRE is running. The specification for the appropriate version 4632N/A * is obtained from either the manifest of a jar file (preferred) or 4632N/A * from command line options. 4632N/A * The routine also parses splash screen command line options and 4632N/A * passes on their values in private environment variables. 4632N/A * If the version has already been selected, set *main_class 4632N/A * with the value passed through the environment (if any) and 4632N/A * Scan through the arguments for options relevant to multiple JRE 4632N/A * support. For reference, the command line syntax is defined as: 4632N/A * java [options] class [argument...] 4632N/A * java [options] -jar file.jar [argument...] 4632N/A * As the scan is performed, make a copy of the argument list with 4632N/A * the version specification options (new to 1.5) removed, so that 4632N/A * a version less than 1.5 can be exec'd. 4632N/A * Note that due to the syntax of the native Windows interface 4632N/A * CreateProcess(), processing similar to the following exists in 4632N/A * Changes here should be reproduced there. 4632N/A /* deal with "unfortunate" classpath syntax */ 4632N/A * Checking for headless toolkit option in the some way as AWT does: 4632N/A * "true" means true and any other value means false 4632N/A if (
argc <= 0) {
/* No operand? Possibly legit with -[full]version */ 4632N/A while (
argc-- > 0)
/* Copy over [argument...] */ 4632N/A * If there is a jar file, read the manifest. If the jarfile can't be 4632N/A * read, the manifest can't be read from the jar file, or the manifest 4632N/A * is corrupt, issue the appropriate error messages and exit. 4632N/A * Even if there isn't a jar file, construct a manifest_info structure 4632N/A * containing the command line information. It's a convenient way to carry 4632N/A * Command line splash screen option should have precedence 4632N/A * over the manifest, so the manifest data is used only if 4632N/A * splash_file_name has not been initialized above during command 4632N/A * Passing on splash screen info in environment variables 4632N/A * The JRE-Version and JRE-Restrict-Search values (if any) from the 4632N/A * manifest are overwritten by any specified on the command line. 4632N/A * "Valid" returns (other than unrecoverable errors) follow. Set 4632N/A * main_class as a side-effect of this routine. 4632N/A * If no version selection information is found either on the command 4632N/A * line or in the manifest, simply return. 4632N/A * Check for correct syntax of the version specification (JSR 56). 4632N/A * Find the appropriate JVM on the system. Just to be as forgiving as 4632N/A * possible, if the standard algorithms don't locate an appropriate 4632N/A * jre, check to see if the one running will satisfy the requirements. 4632N/A * This can happen on systems which haven't been set-up for multiple 4632N/A * If I'm not the chosen one, exec the chosen one. Returning from 4632N/A * ExecJRE indicates that I am indeed the chosen one. 4632N/A * The private environment variable _JAVA_VERSION_SET is used to 4632N/A * prevent the chosen one from re-reading the manifest file and 4632N/A * using the values found within to override the (potential) command 4632N/A * line flags stripped from argv (because the target may not 4632N/A * understand them). Passing the MainClass value is an optimization 4632N/A * to avoid locating, expanding and parsing the manifest extra * Parses command line arguments. Returns JNI_FALSE if launcher * should exit without starting vm, returns JNI_TRUE if vm needs * to be started to process given options. *pret (the launcher * process return value) is set to 0 for a normal exit. * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT. * In the latter case, any SUBOPT value not recognized will default to "all" * The following case provide backward compatibility with old-style sprintf(
tmp,
"-Xrunhprof:cpu=old,file=%s", p +
1);
sprintf(
tmp,
"-Xrunhprof:cpu=old,file=java.prof");
/* No longer supported */ ;
/* Ignore machine independent options already handled */ ;
/* Do not pass option to vm. */ /* default to LM_CLASS if -jar and -cp option are * Initializes the Java Virtual Machine. Also frees options array when printf(
"ignoreUnrecognized is %s, ",
printf(
" option[%2d] = '%s'\n",
* Returns a new Java string object for the specified platform string. /*If the encoding specified in sun.jnu.encoding is not endorsed by "Charset.isSupported" we have to fall back to use String(byte[]) explicitly here without specifying the encoding name, in which the StringCoding class will pickup the iso-8859-1 as the fallback converter for us. * Returns a new array of Java string objects for the specified * array of platform strings. for (i = 0; i <
strc; i++) {
* Loads a class and verifies that the main class is present and it is ok to * call it for more details refer to the java implementation. printf(
"%ld micro seconds to load main class\n",
printf(
"----_JAVA_LAUNCHER_DEBUG----\n");
* For tools, convert command line args thus: * javac -cp foo:foo/"*" -J-ms32m ... * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ... * Takes 4 parameters, and returns the populated arguments /* Copy the VM arguments (i.e. prefixed with -J) */ for (i = 0; i <
jargc; i++) {
if (
arg[0] ==
'-' &&
arg[
1] ==
'J') {
for (i = 0; i <
argc; i++) {
if (
arg[0] ==
'-' &&
arg[
1] ==
'J') {
/* Copy the rest of the arguments */ for (i = 0; i <
jargc ; i++) {
if (
arg[0] !=
'-' ||
arg[
1] !=
'J') {
for (i = 0; i <
argc; i++) {
* For our tools, we try to add 3 VM options: * <envcp> is the user's setting of CLASSPATH -- for instance the user * tells javac where to find binary classes through this environment * variable. Notice that users will be able to compile against our * tools.jar to CLASSPATH. * <apphome> is the directory where the application is installed. * <appcp> is the classpath to where our apps' classfiles are. const char *s =
getenv(
"CLASSPATH");
/* 40 for -Denv.class.path= */ /* How big is the application's classpath? */ for (i = 0; i <
cpathc; i++) {
for (i = 0; i <
cpathc; i++) {
* inject the -Dsun.java.command pseudo property into the args structure * this pseudo property is used in the HotSpot VM to expose the * Java class name and arguments to the main method to the VM. The * HotSpot VM uses this pseudo property to store the Java class name * (or jar file name) and the arguments to the class's main method * to the instrumentation memory region. The sun.java.command pseudo * property is not exported by HotSpot to the Java layer. /* unexpected, one of these should be set. just return without /* determine the amount of memory to allocate assuming * the individual components will be space separated for (i = 0; i <
argc; i++) {
/* allocate the memory */ /* build the -D string */ for (i = 0; i <
argc; i++) {
/* the components of the string are space separated. In * the case of embedded white space, the relationship of * the white space separated components to their true * positional arguments will be ambiguous. This issue may * be addressed in a future release. * JVM would like to know if it's created by a standard Sun launcher, or by * user native application, the following property indicates the former. * Prints the version information from the java.version and other properties. * Prints all the Java settings, see the java implementation for more details. "printXUsageMessage",
"(Z)V"));
"appendVmSynonymMessage",
"printHelpMessage",
"(Z)V"));
/* Initialize the usage message with the usual preamble */ /* Assemble the other variant part of the usage */ /* The first known VM is the default */ /* Complete the usage message and print to stderr*/ * Read the jvm.cfg file and fill the knownJVMs[] array. * The functionality of the jvm.cfg file is subject to change without * notice and the mechanism will be removed in the future. * The lexical structure of the jvm.cfg file is as follows: * knownLine := flag "KNOWN" EOL * warnLine := flag "WARN" EOL * ignoreLine := flag "IGNORE" EOL * errorLine := flag "ERROR" EOL * aliasLine := flag "ALIASED_TO" flag EOL * predicateLine := flag "IF_SERVER_CLASS" flag EOL * commentLine := "#" text EOL * The semantics are that when someone specifies a flag on the command line: * - if the flag appears on a knownLine, then the identifier is used as * the name of the directory holding the JVM library (the name of the JVM). * - if the flag appears as the first flag on an aliasLine, the identifier * of the second flag is used as the name of the JVM. * - if the flag appears on a warnLine, the identifier is used as the * name of the JVM, but a warning is generated. * - if the flag appears on an ignoreLine, the identifier is recognized as the * name of a JVM, but the identifier is ignored and the default vm used * - if the flag appears on an errorLine, an error is generated. * - if the flag appears as the first flag on a predicateLine, and * the machine on which you are running passes the predicate indicated, * then the identifier of the second flag is used as the name of the JVM, * otherwise the identifier of the first flag is used as the name of the JVM. * If no flag is given on the command line, the first vmLine of the jvm.cfg * file determines the name of the JVM. * PredicateLines are only interpreted on first vmLine of a jvm.cfg file, * since they only make sense if someone hasn't specified the name of the * JVM on the command line. * The intent of the jvm.cfg file is to allow several JVM libraries to * be installed in different subdirectories of a single JRE installation, * for space-savings and convenience in testing. * The intent is explicitly not to provide a full aliasing or predicate /* Null-terminate this string for JLI_StringDup below */ /* Null terminate altVMName */ /* Null terminate server class VM name */ /* Returns index of VM or -1 if not found */ * Displays the splash screen according to the jar file name * and image file names stored in environment variables * Done with all command line processing and potential re-execs so * clean up the environment. * If user doesn't specify stack size, check if VM has a preference. * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will * return its default stack size through the init args structure. {
/* Create a new thread to create JVM and invoke main method */ /* If the caller has deemed there is an error we * simply return that, otherwise we return the value of printf(
"NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
printf(
"ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
printf(
"DEFAULT_ERGONOMICS_POLICY\n");
* Return JNI_TRUE for an option string that has no effect but should * _not_ be passed on to the vm; return JNI_FALSE otherwise. On * Solaris SPARC, this screening needs to be done if: * -d32 or -d64 is passed to a binary with an unmatched data model * (the exec in CreateExecutionEnvironment removes -d<n> options and points the * exec to the proper binary). In the case of when the data model and the * requested version is matched, an exec would not occur, and these options * were erroneously passed to the vm. * Unconditionally remove both -d32 and -d64 options since only * the last such options has an effect; e.g. * java -d32 -d64 -d32 -version * A utility procedure to always print to stderr