Exemple #1
0
/*
 * Retrieve settings from registry for current runtime version. Returns
 * 0 if successful otherwise returns -1 if no installed runtime was found
 * or the registry data was invalid.
 */
jint
JRE_GetCurrentSettings(JRESettings *set)
{
    jint r = -1;
    HKEY key;

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) == 0) {
	char *ver = GetStringValue(key, "CurrentVersion");
	if (ver != 0) {
	    r = JRE_GetSettings(set, ver);
	}
	free(ver);
	RegCloseKey(key);
    }
    return r;
}
Exemple #2
0
/*
 * Main program to invoke Java runtime using JNI invocation API. Supports
 * setting of VM arguments through standard command line options.
 */
void main(int argc, char *argv[])
{
	bool	isJava2 = false;
    JRESettings set;
    void *handle;
    JDK1_1InitArgs vmargs;
    JavaVM *jvm;
    JNIEnv *env;
//    char *s, *name;
    jclass cls;
    jmethodID mid;
    jarray args;
    int i;



    /* First scan arguments for help and debug options */
    for (i = 1; i < argc; i++) {
	char *arg = argv[i];
	if (*arg++ != '-') {
	    break;
	}
	if (strcmp(arg, "?") == 0 || strcmp(arg, "h") == 0 ||
	    strcmp(arg, "help") == 0) {
	    PrintUsage();
	    exit(1);
	}
#ifdef JRE_DEBUG
	if (strcmp(arg, "d") == 0) {
	    debug = JNI_TRUE;
	}
#endif
    }

    /* Get runtime settings */
#ifdef VERSION
    if (JRE_GetSettings(&set, VERSION) != 0) {
#else
    if (JRE_GetCurrentSettings(&set) != 0) {
#endif
		if (JRE_GetDefaultSettings(&set) != 0) {
//	    		fprintf(stderr, "Could not locate Java runtime\n");
	    		MessageBox(NULL,"Could not locate Java runtime","",MB_OK);
	    		exit(1);
		}
    }
    short majorVers = 0;
    short minorVers = 0;
    if(set.majorVersion){
    	majorVers = atoi(set.majorVersion);
    }
    if(set.minorVersion){
    	minorVers = atoi(set.minorVersion);
    }
    isJava2 = ((majorVers > 1) || (minorVers > 1));

#ifdef JRE_DEBUG
    if (debug) {
	char *ver = JRE_MakeVersion(set.majorVersion, set.minorVersion,
				    set.microVersion);
	fprintf(stderr, "Runtime Settings:\n");
	fprintf(stderr, " javaHome   = %s\n",
		set.javaHome != 0 ? set.javaHome : "<not set>");
	fprintf(stderr, " runtimeLib = %s\n",
		set.runtimeLib != 0 ? set.runtimeLib : "<not set>");
	fprintf(stderr, " version    = %s\n", ver != 0 ? ver : "<not set>");
	fprintf(stderr, " compiler   = %s\n\n",
		set.compiler != 0 ? set.compiler : "<not set>");
    }
#endif

    /* Load runtime library */
//    char testNetscapeJVMPATH[1024];
//    strcpy(testNetscapeJVMPATH,"C:\\Program Files\\Netscape\\Communicator\\Program\\jrt3240.dll");
	
//    handle = JRE_LoadLibrary(testNetscapeJVMPATH);
    handle = JRE_LoadLibrary(set.runtimeLib);
    if (handle == 0) {
  //winerror.h
    	DWORD err = GetLastError();
//		fprintf(stderr, "Could not load runtime library: %s error %u\n",set.runtimeLib,err);
		char messageStr[1024];
		sprintf(messageStr,"Could not load runtime library: %s error %u",set.runtimeLib,err);
		MessageBox(NULL,messageStr,"",MB_OK);
		exit(1);
    }

   		/* Parse command line options */
    	--argc; argv++;
		if (ParseOptions(&argc, &argv, &vmargs) != 0) {
			PrintUsage();
			exit(1);
    	}

    	/* Get name of class */
    	if (*argv == 0) {
			PrintUsage();
			exit(1);
    	}
/*
    	name = (char *)_strdup(*argv++);
    	for (s = name; *s != '\0'; s++) {
			if (*s == '.') *s = '/';
    	}
    	--argc;
*/
		
    /*
     * The following is used to specify that we require at least
     * JNI version 1.1. Currently, this field is not checked but
     * will be starting with JDK/JRE 1.2. The value returned after
     * calling JNI_GetDefaultJavaVMInitArgs() is the actual JNI version
     * supported, and is always higher that the requested version.
     */
     
	bool	errorCreateVM = true;
    if(isJava2){
	    char *newPath = (char *)JRE_Malloc(strlen("-Djava.class.path=")+strlen(addClassPath)+strlen(set.classPath)+1);// for symbol ;
	    strcpy(newPath,"-Djava.class.path=");
		strcat(newPath,addClassPath);
	    strcat(newPath,set.classPath);
//	 	char *newLibPath = (char *)JRE_Malloc(strlen((char *)nativeLibPath)+strlen("-Djava.library.path=")+1);
	
	 	JavaVMInitArgs vm_args;
		JavaVMOption options[1];
	
	    options[0].optionString = newPath; /* user classes */
//		options[1].optionString = newLibPath;  /* set native library path */
//  	options[2].optionString = "-Djava.compiler=NONE";           /* disable JIT */
//  	options[3].optionString = "-verbose:jni";                   /* print JNI-related messages */

    	vm_args.version = JNI_VERSION_1_2;
    	vm_args.options = options;
    	vm_args.nOptions = 1;
    	vm_args.ignoreUnrecognized = JNI_FALSE;
    	errorCreateVM = (JRE_CreateJavaVM((HINSTANCE)handle, &jvm, &env, &vm_args) != 0);
  		free(newPath);
// 		free(newLibPath);
    }else{ 
    /* Add pre-defined system properties */
    	if (set.javaHome != 0) {
			char *def = (char *)JRE_Malloc(strlen(set.javaHome) + 16);
			sprintf(def, "java.home=%s", set.javaHome);
			AddProperty(def);
    	}
    	if (set.compiler != 0) {
			char *def = (char *)JRE_Malloc(strlen(set.compiler) + 16);
			sprintf(def, "java.compiler=%s", set.compiler);
			AddProperty(def);
    	}
	    vmargs.version = 0x00010001;
		char newPath[1024];
		strcpy(newPath,".;");
		strcat(newPath,addClassPath);
		strcat(newPath,set.classPath);

	    if (JRE_GetDefaultJavaVMInitArgs(handle, &vmargs) != 0) {
//			fprintf(stderr, "Could not initialize Java VM\n");
			MessageBox(NULL,"Could not initialize Java VM","",MB_OK);
			exit(1);
	    }
	    vmargs.classpath = newPath;


#ifdef JRE_DEBUG
    	if (debug) {
			fprintf(stderr, "CLASSPATH is %s\n\n", vmargs.classpath);
    	}
#endif

    	/* Set user-defined system properties for Java VM */
		if (props != 0) {
			if (numProps == maxProps) {
	    		char **tmp = (char **)JRE_Malloc((numProps + 1) * sizeof(char **));
	    		memcpy(tmp, props, numProps * sizeof(char **));
	    		free(props);
	    		props = tmp;
			}
			props[numProps] = 0;
			vmargs.properties = props;
    	}
    	/* Load and initialize Java VM */
    	errorCreateVM = (JRE_CreateJavaVM(handle, &jvm, &env, &vmargs) != 0);
    }

    if (errorCreateVM) {
//		fprintf(stderr, "Could not create Java VM\n");
		MessageBox(NULL,"Could not create Java VM","",MB_OK);
		exit(1);
   	 }

    /* Free properties */
    if (props != 0) {
		free(props);
    }

    /* Find class */
    cls = env->FindClass(mainWabaClassName);
    if (cls == 0) {
//	fprintf(stderr, "Class not found: %s\n", *--argv);
		char messageStr[1024];
		sprintf(messageStr,"Class not found: %s", *--argv);
		MessageBox(NULL,messageStr,"",MB_OK);
	exit(1);
    }

    /* Find main method of class */
    mid = env->GetStaticMethodID( cls, "main",
    				    "([Ljava/lang/String;)V");
    if (mid == 0) {
//	fprintf(stderr, "In class %s: public static void main(String args[]) is not defined\n",mainWabaClassName);
		char messageStr[1024];
		sprintf(messageStr,"In class %s: public static void main(String args[]) is not defined\n",mainWabaClassName);
		MessageBox(NULL,messageStr,"",MB_OK);
	exit(1);
    }

    /* Invoke main method */
    args = NewStringArray(env, argv, argc);

    if (args == 0) {
	JRE_FatalError(env, "Couldn't build argument list for main\n");
    }
    env->CallStaticVoidMethod(cls, mid, args);
    if (env->ExceptionOccurred()) {
	env->ExceptionDescribe();
    }

    /* Wait until we are the only user thread remaining then unload VM */
    jvm->DestroyJavaVM();

    /* Unload the runtime */
    JRE_UnloadLibrary(handle);
}

/*
 * Parses command line VM options. Returns 0 if successful otherwise
 * returns -1 if an invalid option was encountered.
 */
jint ParseOptions(int *argcp, char ***argvp, JDK1_1InitArgs *vmargs)
{
    char *arg, **argv = *argvp;

    while ((arg = *argv++) != 0 && *arg++ == '-') {
	if (strcmp(arg, "classpath") == 0) {
	    if (*argv == 0) {
//		fprintf(stderr, "No class path given for %s option\n", arg);
		char messageStr[1024];
		sprintf(messageStr,"No class path given for %s option", arg);
		MessageBox(NULL,messageStr,"",MB_OK);
		return -1;
	    }
	    vmargs->classpath = *argv++;
	} else if (strcmp(arg, "cp") == 0) {
	    char *cp = vmargs->classpath;
	    if (*argv == 0) {
//		fprintf(stderr, "No class path given for %s option\n", arg);
		char messageStr[1024];
		sprintf(messageStr,"No class path given for %s option", arg);
		MessageBox(NULL,messageStr,"",MB_OK);
		return -1;
	    }
	    vmargs->classpath = (char *)malloc(strlen(*argv) + strlen(cp) + 2);
	    if (vmargs->classpath == 0) {
		perror("malloc");
		exit(1);
	    }
	    sprintf(vmargs->classpath, "%s%c%s", *argv++, PATH_SEPARATOR, cp);
	} else if (strncmp(arg, "D", 1) == 0) {
	    AddProperty(arg + 1);
	} else if (strncmp(arg, "ss", 2) == 0) {
	    jint n = atoml(arg + 2);
	    if (n >= 1000) {
		vmargs->nativeStackSize = n;
	    }
	} else if (strncmp(arg, "oss", 3) == 0) {
	    jint n = atoml(arg + 3);
	    if (n >= 1000) {
		vmargs->javaStackSize = n;
	    }
	} else if (strncmp(arg, "ms", 2) == 0) {
	    jint n = atoml(arg + 2);
	    if (n >= 1000) {
		vmargs->minHeapSize = n;
	    }
	} else if (strncmp(arg, "mx", 2) == 0) {
	    jint n = atoml(arg + 2);
	    if (n >= 1000) {
		vmargs->maxHeapSize = n;
	    }
	} else if (strcmp(arg, "noasyncgc") == 0) {
	    vmargs->disableAsyncGC = JNI_TRUE;
	} else if (strcmp(arg, "noclassgc") == 0) {
	    vmargs->enableClassGC = JNI_FALSE;
	} else if (strcmp(arg, "verify") == 0) {
	    vmargs->verifyMode = 2;
	} else if (strcmp(arg, "verifyremote") == 0) {
	    vmargs->verifyMode = 1;
	} else if (strcmp(arg, "noverify") == 0) {
	    vmargs->verifyMode = 0;
	} else if (strcmp(arg, "nojit") == 0) {
	    /**
	     * Set the value of java.compiler equal to the empty 
	     * string.  At the jit library loading step nothing will
	     * loaded.
	     */
	    AddProperty("java.compiler=");
	} else if (strcmp(arg, "v") == 0 || strcmp(arg, "verbose") == 0) {
	    vmargs->verbose = JNI_TRUE;
#ifdef JRE_DEBUG
	} else if (strcmp(arg, "d") == 0) {
	    debug = JNI_TRUE;
#endif
	} else if (strcmp(arg, "verbosegc") == 0) {
	    vmargs->enableVerboseGC = JNI_TRUE;
	} else if (strcmp(arg, "?") == 0 || strcmp(arg, "h") == 0 ||
		   strcmp(arg, "help") == 0) {
	    return -1;
	} else {
//	    fprintf(stderr, "Illegal option: -%s\n", arg);
		char messageStr[1024];
		sprintf(messageStr,"Illegal option: -%s", arg);
		MessageBox(NULL,messageStr,"",MB_OK);
	    return -1;
	}
    }
    *argcp -= --argv - *argvp;
    *argvp = argv;
    return 0;
}