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