src/share/bin/java.c

Print this page

        

@@ -92,19 +92,19 @@
 
 /*
  * Prototypes for functions internal to launcher.
  */
 static void SetClassPath(const char *s);
-static void SetModulesBootClassPath(const char *s);
 static void SelectVersion(int argc, char **argv, char **main_class);
-static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
-                               char **pclassname, int *pret, const char *jvmpath);
+static jboolean ParseArguments(int *pargc, char ***pargv,
+                               int *pmode, char **pwhat,
+                               int *pret, const char *jrepath);
 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
                               InvocationFunctions *ifn);
 static jstring NewPlatformString(JNIEnv *env, char *s);
 static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
-static jclass LoadMainClass(JNIEnv *env, jboolean isJar, char *name);
+static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
 
 static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
 static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
 static void SetApplicationClassPath(const char**);
 

@@ -162,15 +162,24 @@
 static jlong maxHeapSize        = 0;  /* max heap size */
 static jlong initialHeapSize    = 0;  /* inital heap size */
 
 int JNICALL JavaMain(void * args); /* entry point                  */
 
+enum LaunchMode {               // cf. sun.launcher.LauncherHelper
+    LM_UNKNOWN = 0,
+    LM_CLASS,
+    LM_JAR
+};
+
+static const char *launchModeNames[]
+    = { "Unknown", "Main class", "JAR file" };
+
 typedef struct {
   int     argc;
-  char ** argv;
-  char *  jarfile;
-  char *  classname;
+    char **argv;
+    int    mode;
+    char  *what;
   InvocationFunctions ifn;
 } JavaMainArgs;
 
 /*
  * Entry point.

@@ -187,12 +196,12 @@
         jboolean cpwildcard,                    /* classpath wildcard*/
         jboolean javaw,                         /* windows-only javaw */
         jint ergo                               /* ergonomics class policy */
 )
 {
-    char *jarfile = 0;
-    char *classname = 0;
+    int mode = LM_UNKNOWN;
+    char *what = NULL;
     char *cpath = 0;
     char *main_class = NULL;
     int ret;
     InvocationFunctions ifn;
     jlong start, end;

@@ -275,28 +284,25 @@
             cpath = ".";
         }
         SetClassPath(cpath);
     }
 
-    /*
-     *  Parse command line options; if the return value of
+    /* Parse command line options; if the return value of
      *  ParseArguments is false, the program should exit.
      */
-    if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret, jvmpath)) {
+    if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
+    {
         return(ret);
     }
 
-    /* Set bootclasspath for modules */
-    SetModulesBootClassPath(jrepath);
-
     /* Override class path if -jar flag was specified */
-    if (jarfile != 0) {
-        SetClassPath(jarfile);
+    if (mode == LM_JAR) {
+        SetClassPath(what);     /* Override class path */
     }
 
     /* set the -Dsun.java.command pseudo property */
-    SetJavaCommandLineProp(classname, jarfile, argc, argv);
+    SetJavaCommandLineProp(what, argc, argv);
 
     /* Set the -Dsun.java.launcher pseudo property */
     SetJavaLauncherProp();
 
     /* set the -Dsun.java.launcher.* platform properties */

@@ -303,11 +309,11 @@
     SetJavaLauncherPlatformProps();
 
     /* Show the splash screen if needed */
     ShowSplashScreen();
 
-    return ContinueInNewThread(&ifn, argc, argv, jarfile, classname, ret);
+    return ContinueInNewThread(&ifn, argc, argv, mode, what, ret);
 
 }
 /*
  * Always detach the main thread so that it appears to have ended when
  * the application's main method exits.  This will invoke the

@@ -351,17 +357,17 @@
 JavaMain(void * _args)
 {
     JavaMainArgs *args = (JavaMainArgs *)_args;
     int argc = args->argc;
     char **argv = args->argv;
-    char *jarfile = args->jarfile;
-    char *classname = args->classname;
+    int mode = args->mode;
+    char *what = args->what;
     InvocationFunctions ifn = args->ifn;
 
     JavaVM *vm = 0;
     JNIEnv *env = 0;
-    jclass mainClass;
+    jclass mainClass = NULL;
     jmethodID mainID;
     jobjectArray mainArgs;
     int ret = 0;
     jlong start, end;
 

@@ -383,11 +389,11 @@
     if (showSettings != NULL) {
         ShowSettings(env, showSettings);
         CHECK_EXCEPTION_LEAVE(1);
     }
     /* If the user specified neither a class name nor a JAR file */
-    if (printXUsage || printUsage || (jarfile == 0 && classname == 0)) {
+    if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
         PrintUsage(env, printXUsage);
         CHECK_EXCEPTION_LEAVE(1);
         LEAVE();
     }
 

@@ -397,15 +403,15 @@
         end = CounterGet();
         JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
                (long)(jint)Counter2Micros(end-start));
     }
 
-    /* At this stage, argc/argv have the applications' arguments */
+    /* At this stage, argc/argv have the application's arguments */
     if (JLI_IsTraceLauncher()){
         int i;
-        printf("Main-Class is '%s'\n", classname ? classname : "");
-        printf("Apps' argc is %d\n", argc);
+        printf("%s is '%s'\n", launchModeNames[mode], what);
+        printf("App's argc is %d\n", argc);
         for (i=0; i < argc; i++) {
             printf("    argv[%2d] = '%s'\n", i, argv[i]);
         }
     }
 

@@ -429,15 +435,11 @@
      *          Main-Class attribute gets properly passed through
      *          all environments,
      *     2)   Remove the vestages of maintaining main_class through
      *          the environment (and remove these comments).
      */
-    if (jarfile != 0) {
-        mainClass = LoadMainClass(env, JNI_TRUE, jarfile);
-    } else {
-        mainClass = LoadMainClass(env, JNI_FALSE, classname);
-    }
+    mainClass = LoadMainClass(env, mode, what);
     CHECK_EXCEPTION_NULL_LEAVE(mainClass);
 
     /*
      * The LoadMainClass not only loads the main class, it will also ensure
      * that the main method's signature is correct, therefore further checking

@@ -717,48 +719,10 @@
     if (s != orig)
         JLI_MemFree((char *) s);
 }
 
 /*
- * Set the bootclasspath for modules.
- * A temporary workaround until jigsaw is integrated into JDK 7.
- */
-static void
-SetModulesBootClassPath(const char *jrepath)
-{
-    char *def, *s;
-    char pathname[MAXPATHLEN];
-    const char separator[] = { FILE_SEPARATOR, '\0' };
-    const char *orig = jrepath;
-    static const char format[] = "-Xbootclasspath/p:%s";
-    struct stat statbuf;
-
-    /* return if jre/lib/rt.jar exists */
-    JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%srt.jar", jrepath, separator, separator);
-    if (stat(pathname, &statbuf) == 0) {
-        return;
-    }
-
-    /* return if jre/classes exists */
-    JLI_Snprintf(pathname, sizeof(pathname), "%s%sclasses", jrepath, separator);
-    if (stat(pathname, &statbuf) == 0) {
-        return;
-    }
-
-    /* modularized jre */
-    JLI_Snprintf(pathname, sizeof(pathname), "%s%slib%s*", jrepath, separator, separator);
-    s = (char *) JLI_WildcardExpandClasspath(pathname);
-    def = JLI_MemAlloc(sizeof(format)
-                       - 2 /* strlen("%s") */
-                       + JLI_StrLen(s));
-    sprintf(def, format, s);
-    AddOption(def, NULL);
-    if (s != orig)
-        JLI_MemFree((char *) s);
-}
-
-/*
  * The SelectVersion() routine ensures that an appropriate version of
  * the JRE is running.  The specification for the appropriate version
  * is obtained from either the manifest of a jar file (preferred) or
  * from command line options.
  * The routine also parses splash screen command line options and

@@ -1002,29 +966,31 @@
  * should exit without starting vm, returns JNI_TRUE if vm needs
  * to be started to process  given options. *pret (the launcher
  * process return value) is set to 0 for a normal exit.
  */
 static jboolean
-ParseArguments(int *pargc, char ***pargv, char **pjarfile,
-                       char **pclassname, int *pret, const char *jvmpath)
+ParseArguments(int *pargc, char ***pargv,
+               int *pmode, char **pwhat,
+               int *pret, const char *jrepath)
 {
     int argc = *pargc;
     char **argv = *pargv;
-    jboolean jarflag = JNI_FALSE;
+    int mode = LM_UNKNOWN;
     char *arg;
 
     *pret = 0;
 
     while ((arg = *argv) != 0 && *arg == '-') {
         argv++; --argc;
         if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
             ARG_CHECK (argc, ARG_ERROR1, arg);
             SetClassPath(*argv);
+            mode = LM_CLASS;
             argv++; --argc;
         } else if (JLI_StrCmp(arg, "-jar") == 0) {
             ARG_CHECK (argc, ARG_ERROR2, arg);
-            jarflag = JNI_TRUE;
+            mode = LM_JAR;
         } else if (JLI_StrCmp(arg, "-help") == 0 ||
                    JLI_StrCmp(arg, "-h") == 0 ||
                    JLI_StrCmp(arg, "-?") == 0) {
             printUsage = JNI_TRUE;
             return JNI_TRUE;

@@ -1100,23 +1066,28 @@
             AddOption(arg, NULL);
         }
     }
 
     if (--argc >= 0) {
-        if (jarflag) {
-            *pjarfile = *argv++;
-            *pclassname = NULL;
-        } else {
-            *pjarfile = NULL;
-            *pclassname = *argv++;
+        *pwhat = *argv++;
         }
+
+    if (*pwhat == NULL) {
+        *pret = 1;
+    } else if (mode == LM_UNKNOWN) {
+        /* default to LM_CLASS if -jar and -cp option are
+         * not specified */
+        mode = LM_CLASS;
+    }
+
+    if (argc >= 0) {
         *pargc = argc;
         *pargv = argv;
     }
-    if (*pjarfile == NULL && *pclassname == NULL) {
-        *pret = 1;
-    }
+
+    *pmode = mode;
+
     return JNI_TRUE;
 }
 
 /*
  * Initializes the Java Virtual Machine. Also frees options array when

@@ -1261,11 +1232,11 @@
 /*
  * Loads a class and verifies that the main class is present and it is ok to
  * call it for more details refer to the java implementation.
  */
 static jclass
-LoadMainClass(JNIEnv *env, jboolean isJar, char *name)
+LoadMainClass(JNIEnv *env, int mode, char *name)
 {
     jclass cls;
     jmethodID mid;
     jstring str;
     jobject result;

@@ -1274,13 +1245,13 @@
     if (JLI_IsTraceLauncher()) {
         start = CounterGet();
     }
     NULL_CHECK0(cls = FindBootStrapClass(env, "sun/launcher/LauncherHelper"));
     NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls, "checkAndLoadMain",
-                                          "(ZZLjava/lang/String;)Ljava/lang/Object;"));
+                                          "(ZILjava/lang/String;)Ljava/lang/Class;"));
     str = (*env)->NewStringUTF(env, name);
-    result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, isJar, str);
+    result = (*env)->CallStaticObjectMethod(env, cls, mid, JNI_TRUE, mode, str);
 
     if (JLI_IsTraceLauncher()) {
         end   = CounterGet();
         printf("%ld micro seconds to load main class\n",
                (long)(jint)Counter2Micros(end-start));

@@ -1422,35 +1393,29 @@
  * (or jar file name) and the arguments to the class's main method
  * to the instrumentation memory region. The sun.java.command pseudo
  * property is not exported by HotSpot to the Java layer.
  */
 void
-SetJavaCommandLineProp(char *classname, char *jarfile,
-                       int argc, char **argv)
+SetJavaCommandLineProp(char *what, int argc, char **argv)
 {
 
     int i = 0;
     size_t len = 0;
     char* javaCommand = NULL;
     char* dashDstr = "-Dsun.java.command=";
 
-    if (classname == NULL && jarfile == NULL) {
+    if (what == NULL) {
         /* unexpected, one of these should be set. just return without
          * setting the property
          */
         return;
     }
 
-    /* if the class name is not set, then use the jarfile name */
-    if (classname == NULL) {
-        classname = jarfile;
-    }
-
     /* determine the amount of memory to allocate assuming
      * the individual components will be space separated
      */
-    len = JLI_StrLen(classname);
+    len = JLI_StrLen(what);
     for (i = 0; i < argc; i++) {
         len += JLI_StrLen(argv[i]) + 1;
     }
 
     /* allocate the memory */

@@ -1457,11 +1422,11 @@
     javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
 
     /* build the -D string */
     *javaCommand = '\0';
     JLI_StrCat(javaCommand, dashDstr);
-    JLI_StrCat(javaCommand, classname);
+    JLI_StrCat(javaCommand, what);
 
     for (i = 0; i < argc; i++) {
         /* the components of the string are space separated. In
          * the case of embedded white space, the relationship of
          * the white space separated components to their true

@@ -1477,11 +1442,12 @@
 
 /*
  * JVM would like to know if it's created by a standard Sun launcher, or by
  * user native application, the following property indicates the former.
  */
-void SetJavaLauncherProp() {
+void
+SetJavaLauncherProp() {
   AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
 }
 
 /*
  * Prints the version information from the java.version and other properties.

@@ -1911,12 +1877,12 @@
 {
     return _wc_enabled;
 }
 
 static int
-ContinueInNewThread(InvocationFunctions* ifn, int argc,
-                     char **argv, char *jarfile, char *classname, int ret)
+ContinueInNewThread(InvocationFunctions* ifn, int argc, char **argv,
+                    int mode, char *what, int ret)
 {
 
     /*
      * If user doesn't specify stack size, check if VM has a preference.
      * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will

@@ -1936,12 +1902,12 @@
       JavaMainArgs args;
       int rslt;
 
       args.argc = argc;
       args.argv = argv;
-      args.jarfile = jarfile;
-      args.classname = classname;
+      args.mode = mode;
+      args.what = what;
       args.ifn = *ifn;
 
       rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
       /* If the caller has deemed there is an error we
        * simply return that, otherwise we return the value of