⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java.c

📁 用bcg库编写的java IDE 源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * @(#)java.c	1.91 01/12/03 * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * Shared source for 'java' command line tool. * * If JAVA_ARGS is defined, then acts as a launcher for applications. For * instance, the JDK command line tools such as javac and javadoc (see * makefiles for more details) are built with this program.  Any arguments * prefixed with '-J' will be passed directly to the 'java' command. */
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifndef WIN32#include <unistd.h>#endif#include <jni.h>#include "java.h"#ifndef FULL_VERSION#define FULL_VERSION "1.4"#endif#ifdef WIN32#define PATHSEP "\\"#else#define PATHSEP "/"#endifstatic jboolean printVersion = JNI_FALSE; /* print and exit */static jboolean showVersion = JNI_FALSE;  /* print but continue */static char *progname;jboolean debug = JNI_FALSE;int      status = 0;/* * List of VM options to be specified when the VM is created. */static JavaVMOption *options;static int numOptions, maxOptions;/* * Prototypes for functions internal to launcher. */static void AddOption(char *str, void *info);static void SetClassPath(char *s);static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,			       char **pclassname, int *pret);static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,			      InvocationFunctions *ifn);static void* MemAlloc(size_t size);static jstring NewPlatformString(JNIEnv *env, char *s);static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);static jstring NewPlatformString(JNIEnv *env, char *s);static jclass LoadClass(JNIEnv *env, char *name);static jstring GetMainClassName(JNIEnv *env, char *jarname);#ifdef JAVA_ARGSstatic void TranslateDashJArgs(int *pargc, char ***pargv);static jboolean AddApplicationOptions(void);#endifstatic void PrintJavaVersion(JNIEnv *env);static void PrintUsage(void);static jint PrintXUsage(void);static char *SetExecname(int argc, char **argv);static void SetPaths(int argc, char **argv);static char *CheckJvmType(int *argc, char ***argv);static void SetDataModel(int *argc, char ***argv, char *execname, 			    char *jrepath);static void SetLibraryPath(char ** original_argv, char *execname, 			   char *jrepath, char *jvmpath);static void InitEncodingFlag(JNIEnv *env); /* Temp fix for UTF16-le encoding *//* Support for options such as -hotspot, -classic etc. */#define INIT_MAX_KNOWN_VMS 10struct vmdesc {    char *name;#define VM_UNKNOWN -1#define VM_KNOWN 0#define VM_ALIASED_TO 1#define VM_WARN 2#define VM_ERROR 3    int flag;    char *alias;};static struct vmdesc *knownVMs = NULL;static int knownVMsCount = 0;static int knownVMsLimit = 0;static jint ReadKnownVMs(const char *jrepath); static void GrowKnownVMs();static int  KnownVMIndex(const char* name);static void FreeKnownVMs(); /* * Entry point. */intmain(int argc, char ** argv){    JavaVM *vm = 0;    JNIEnv *env = 0;    char *jarfile = 0;    char *classname = 0;    char *s = 0;    jclass mainClass;    jmethodID mainID;    jobjectArray mainArgs;    int ret;    InvocationFunctions ifn;    char *jvmtype = 0;    jlong start, end;    char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];    char ** original_argv = argv;#ifndef WIN32    char *execname = 0;#endif    if (getenv("_JAVA_LAUNCHER_DEBUG") != 0) {	debug = JNI_TRUE;	printf("----_JAVA_LAUNCHER_DEBUG----\n");    }#ifndef WIN32    /* Compute the name of the executable */    execname = SetExecname(argc, argv);#endif    /* Find out where the JRE is that we will be using. */    if (!GetJREPath(jrepath, sizeof(jrepath))) {	fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n");	return 2;    }#ifndef WIN32    /* Check for data model flags, and run a different executable, if necessary. */    SetDataModel(&argc, &argv, execname, jrepath);#endif    /* Find the specified JVM type */    if (ReadKnownVMs(jrepath) < 1) {	fprintf(stderr, "Error: no known VMs. (check for corrupt jvm.cfg file)\n");	exit(1);    }    jvmtype = CheckJvmType(&argc, &argv);    jvmpath[0] = '\0';    if (!GetJVMPath(jrepath, jvmtype, jvmpath, sizeof(jvmpath))) {	fprintf(stderr, "Error: no `%s' JVM at `%s'.\n", jvmtype, jvmpath);	return 4;    }    /* If we got here, jvmpath has been correctly initialized. */#ifndef WIN32    /* Set the LD_LIBRARY_PATH environment variable */    SetLibraryPath(original_argv, execname, jrepath, jvmpath);#endif    ifn.CreateJavaVM = 0; ifn.GetDefaultJavaVMInitArgs = 0;    if (!LoadJavaVM(jvmpath, &ifn)) {        status = 1;	return 6;    }    #ifdef JAVA_ARGS  /* javac, jar and friends. */    progname = "java";#else             /* java, oldjava, javaw and friends */#ifdef PROGNAME    progname = PROGNAME;#else    progname = *argv;    if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {	progname = s + 1;    }#endif /* PROGNAME */#endif /* JAVA_ARGS */    ++argv;    --argc;#ifdef JAVA_ARGS    /* Preprocess wrapper arguments */    TranslateDashJArgs(&argc, &argv);    if (!AddApplicationOptions()) {        status = 2;	return 1;    }#endif    /* Set default CLASSPATH */    if ((s = getenv("CLASSPATH")) == 0) {	s = ".";    }#ifndef JAVA_ARGS    SetClassPath(s);#endif    /* Parse command line options */    if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret)) {        status = 2;	return ret;    }    /* Override class path if -jar flag was specified */    if (jarfile != 0) {	SetClassPath(jarfile);    }    /* Initialize the virtual machine */    if (debug)	start = CounterGet();    if (!InitializeJVM(&vm, &env, &ifn)) {	fprintf(stderr, "Could not create the Java virtual machine.\n");        status = 3;	return 1;    }    if (printVersion || showVersion) {        PrintJavaVersion(env);	if ((*env)->ExceptionOccurred(env)) {	    (*env)->ExceptionDescribe(env);	    goto leave;	}	if (printVersion) {	    ret = 0;	    goto leave;	}	if (showVersion) {	    fprintf(stderr, "\n");	}    }    /* If the user specified neither a class name or a JAR file */    if (jarfile == 0 && classname == 0) {	PrintUsage();	goto leave;    }    FreeKnownVMs();  /* after last possible PrintUsage() */    if (debug) {	end   = CounterGet();	printf("%ld micro seconds to InitializeJVM\n",	       (long)(jint)Counter2Micros(end-start));    }    /* At this stage, argc/argv have the applications' arguments */    if (debug) {	int i = 0;	printf("Main-Class is '%s'\n", classname ? classname : "");	printf("Apps' argc is %d\n", argc);	for (; i < argc; i++) {	    printf("    argv[%2d] = '%s'\n", i, argv[i]);	}    }    ret = 1;    /* Temporary fix for utf-16le encoding. Init encoding flag */    InitEncodingFlag(env);    /* Get the application's main class */    if (jarfile != 0) {	jstring mainClassName = GetMainClassName(env, jarfile);	if ((*env)->ExceptionOccurred(env)) {	    (*env)->ExceptionDescribe(env);	    goto leave;	}	if (mainClassName == NULL) {	    fprintf(stderr, "Failed to load Main-Class manifest attribute "		    "from\n%s\n", jarfile);	    goto leave;	}	classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);	if (classname == NULL) {	    (*env)->ExceptionDescribe(env);	    goto leave;	}	mainClass = LoadClass(env, classname);	(*env)->ReleaseStringUTFChars(env, mainClassName, classname);    } else {	mainClass = LoadClass(env, classname);    }    if (mainClass == NULL) {        (*env)->ExceptionDescribe(env);        status = 4;	goto leave;    }    /* Get the application's main method */    mainID = (*env)->GetStaticMethodID(env, mainClass, "main",				       "([Ljava/lang/String;)V");    if (mainID == NULL) {	if ((*env)->ExceptionOccurred(env)) {	    (*env)->ExceptionDescribe(env);	} else {	    fprintf(stderr, "No main method found in specified class.\n");	}        status = 5;	goto leave;    }    {    /* Make sure the main method is public */	jobject obj = (*env)->ToReflectedMethod(env, mainClass, 						mainID, JNI_TRUE);	jint mods;	jmethodID mid = 	  (*env)->GetMethodID(env, 			      (*env)->GetObjectClass(env, obj),			      "getModifiers", "()I");	if ((*env)->ExceptionOccurred(env)) {	    (*env)->ExceptionDescribe(env);	    status = 6;	    goto leave;	}	mods = (*env)->CallIntMethod(env, obj, mid);	if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */	    fprintf(stderr, "Main method not public.\n");	    status = 8;	    goto leave;	}    }    /* Build argument array */    mainArgs = NewPlatformStringArray(env, argv, argc);    if (mainArgs == NULL) {	(*env)->ExceptionDescribe(env);	goto leave;    }    /* Invoke main method. */    (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);    if ((*env)->ExceptionOccurred(env)) {	/* Formerly, we used to call the "uncaughtException" method of the	   main thread group, but this was later shown to be unnecessary	   since the default definition merely printed out the same exception	   stack trace as ExceptionDescribe and could never actually be	   overridden by application programs. */	(*env)->ExceptionDescribe(env);	goto leave;    }    /*     * Detach the current thread so that it appears to have exited when     * the application's main method exits.     */    if ((*vm)->DetachCurrentThread(vm) != 0) {	fprintf(stderr, "Could not detach main thread.\n");	goto leave;    }    ret = 0;leave:    (*vm)->DestroyJavaVM(vm);    return ret;}#ifndef WIN32#include <sys/stat.h>/* * Return true if the named program exists */static intProgramExists(char *name){    struct stat sb;    if (stat(name, &sb) != 0) return 0;    if (S_ISDIR(sb.st_mode)) return 0;    return (sb.st_mode & S_IEXEC) != 0;}/* * Find a command in a directory, returning the path. */static char *Resolve(char *indir, char *cmd){    char name[PATH_MAX + 2], *real;    if ((strlen(indir) + strlen(cmd) + 1)  > PATH_MAX) return 0;    sprintf(name, "%s%c%s", indir, FILE_SEPARATOR, cmd);    if (!ProgramExists(name)) return 0;    real = malloc(PATH_MAX + 2);    if (!realpath(name, real)) 	strcpy(real, name);    return real;}/* * Find a path for the executable */static char *FindExecName(char *program){    char cwdbuf[PATH_MAX+2];    char *path;    char *tmp_path;    char *f;    char *result = 0;    /* absolute path? */    if (*program == FILE_SEPARATOR || 	(FILE_SEPARATOR=='\\' && strrchr(program, ':')))	return Resolve("", program+1);    /* relative path? */    if (strrchr(program, FILE_SEPARATOR) != 0) {	char buf[PATH_MAX+2];	return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);    }    /* from search path? */    path = getenv("PATH");    if (!path || !*path) path = ".";    tmp_path = malloc(strlen(path) + 2);    strcpy(tmp_path, path);    for (f=tmp_path; *f && result==0; ) {	char *s = f;	while (*f && (*f != PATH_SEPARATOR)) ++f;	if (*f) *f++ = 0;	if (*s == FILE_SEPARATOR)	    result = Resolve(s, program);	else {	    /* relative path element */	    char dir[2*PATH_MAX];	    sprintf(dir, "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)), 		    FILE_SEPARATOR, s);	    result = Resolve(dir, program);	}	if (result != 0) break;    }    free(tmp_path);    return result;}/* Store the name of the executable once computed */static char *execname = 0;/* * Compute the name of the executable */static char *SetExecname(int argc, char **argv){    char *exec_path = FindExecName(argv[0]);    execname = exec_path;    return exec_path;}/* * Return the name of the executable.  Used in java_md.c to find the JRE area. */char *GetExecname(){    return execname;}#endif /* #ifndef WIN32 *//* * Run another executable if necessary to change the data model. * Also, remove -d64 and the like from argc/argv. */static void

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -