Exemplo n.º 1
0
/*
 * Parses a runtime version string. Returns 0 if the successful, otherwise
 * returns -1 if the format of the version string was invalid.
 */
jint
JRE_ParseVersion(const char *ver, char **majorp, char **minorp, char **microp)
{
    int n1 = 0, n2 = 0, n3 = 0;

    sscanf(ver, "%*[0-9]%n.%*[0-9]%n.%*[0-9a-zA-Z]%n", &n1, &n2, &n3);
    if (n1 == 0 || n2 == 0) {
	return -1;
    }
    if (n3 != 0) {
	if (n3 != (int)strlen(ver)) {
	    return -1;
	}
    } else if (n2 != (int)strlen(ver)) {
	return -1;
    }
    *majorp = JRE_Malloc(n1 + 1);
    strncpy(*majorp, ver, n1);
    (*majorp)[n1] = 0;
    *minorp = JRE_Malloc(n2 - n1);
    strncpy(*minorp, ver + n1 + 1, n2 - n1 - 1);
    (*minorp)[n2 - n1 - 1] = 0;
    if (n3 != 0) {
	*microp = JRE_Malloc(n3 - n2);
	strncpy(*microp, ver + n2 + 1, n3 - n2 - 1);
	(*microp)[n3 - n2 - 1] = 0;
    }
    return 0;
}
Exemplo n.º 2
0
/*
 * Return default runtime library for specified Java home directory.
 */
char *
JRE_GetDefaultRuntimeLib(const char *dir)
{
    char *cp = JRE_Malloc(strlen(dir) + sizeof(RUNTIME_LIB) + 8);
    sprintf(cp, "%s\\bin\\" RUNTIME_LIB, dir);
    return cp;
}
Exemplo n.º 3
0
/*
 * Return default class path for specified Java home directory.
 */
char *
JRE_GetDefaultClassPath(const char *dir)
{
    char *cp = JRE_Malloc(strlen(dir) * 4 + 64);
    sprintf(cp, "%s\\lib\\rt.jar;%s\\lib\\i18n.jar;%s\\lib\\classes.zip;"
		"%s\\classes", dir, dir, dir, dir);
    return cp;
}
Exemplo n.º 4
0
/*
 * Adds a user-defined system property definition.
 */
void AddProperty(char *def)
{
    if (numProps >= maxProps) {
	if (props == 0) {
	    maxProps = 4;
	    props = (char **)JRE_Malloc(maxProps * sizeof(char **));
	} else {
	    char **tmp;
	    maxProps *= 2;
	    tmp = (char **)JRE_Malloc(maxProps * sizeof(char **));
	    memcpy(tmp, props, numProps * sizeof(char **));
	    free(props);
	    props = tmp;
	}
    }
    props[numProps++] = def;
}
Exemplo n.º 5
0
/*
 * Creates a version number string from the specified major, minor, and
 * micro version numbers.
 */
char *
JRE_MakeVersion(const char *major, const char *minor, const char *micro)
{
    char *ver = 0;

    if (major != 0 && minor != 0) {
	int len = strlen(major) + strlen(minor);
	if (micro != 0) {
	    ver = JRE_Malloc(len + strlen(micro) + 3);
	    sprintf(ver, "%s.%s.%s", major, minor, micro);
	} else {
	    ver = JRE_Malloc(len + 2);
	    sprintf(ver, "%s.%s", major, minor);
	}
    }
    return ver;
}
Exemplo n.º 6
0
/*
 * Returns string data for the specified registry value name, or
 * NULL if not found.
 */
static char *
GetStringValue(HKEY key, const char *name)
{
    DWORD type, size;
    char *value = 0;

    if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 &&
	type == REG_SZ ) {
	value = JRE_Malloc(size);
	if (RegQueryValueEx(key, name, 0, 0, value, &size) != 0) {
	    free(value);
	    value = 0;
	}
    }
    return value;
}
Exemplo n.º 7
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;
}