📄 java_tool.cpp
字号:
char **argv = *_argv;
int i, running, wanted;
char **newargv;
int newargc = 0;
newargv = (char **)malloc((argc+1) * sizeof(*newargv));
newargv[newargc++] = argv[0];
#ifdef _LP64
/* we're already running a 64-bit executable */
running = 64;
#else
running = 32;
#endif
wanted = 0;
/* scan for data model arg */
for (i=1; i<argc; i++) {
if (strcmp(argv[i], "-J-d64") == 0 || strcmp(argv[i], "-d64") == 0) {
wanted = 64;
continue;
}
if (strcmp(argv[i], "-J-d32") == 0 || strcmp(argv[i], "-d32") == 0) {
wanted = 32;
continue;
}
newargv[newargc++] = argv[i];
#ifdef JAVA_ARGS
if (argv[i][0] != '-') continue;
#else
if (strcmp(argv[i], "-classpath") == 0 || strcmp(argv[i], "-cp") == 0) {
i++;
if (i >= argc) break;
newargv[newargc++] = argv[i];
continue;
}
if (argv[i][0] != '-') { i++; break; }
#endif
}
/* copy rest of args [i .. argc) */
while (i < argc) {
newargv[newargc++] = argv[i++];
}
newargv[newargc] = 0;
*_argc = argc = newargc;
*_argv = argv = newargv;
if (wanted == 0 || running == wanted) return;
{
char *oldexec = strcpy(malloc(strlen(execname) + 1), execname);
char *olddir = oldexec;
char *oldbase = strrchr(oldexec, '/');
char *newexec;
newexec = malloc(strlen(execname) + 20);
*oldbase++ = 0;
sprintf(newexec, "%s/%s/%s", olddir,
((wanted==64) ? "sparcv9" : ".."), oldbase);
argv[0] = newexec;
execv(newexec, argv);
perror("execv()");
exit(1);
}
#endif /* sparc */
}
/*
* Check for a specified JVM type
*/
static char * CheckJvmType(int *pargc, char ***argv) {
int i, argi;
int argc;
char **newArgv;
int newArgvIdx = 0;
int isVMType;
int jvmidx = -1;
char *jvmtype = getenv("JDK_ALTERNATE_VM");
argc = *pargc;
/* To make things simpler we always copy the argv array */
newArgv = (char**)MemAlloc((argc + 1) * sizeof(char *));
/* The program name is always present */
newArgv[newArgvIdx++] = (*argv)[0];
for (argi = 1; argi < argc; argi++) {
char *arg = (*argv)[argi];
isVMType = 0;
#ifdef JAVA_ARGS
if (arg[0] != '-') {
newArgv[newArgvIdx++] = arg;
continue;
}
#else
if (strcmp(arg, "-classpath") == 0 ||
strcmp(arg, "-cp") == 0) {
newArgv[newArgvIdx++] = arg;
argi++;
if (argi < argc) {
newArgv[newArgvIdx++] = (*argv)[argi];
}
continue;
}
if (arg[0] != '-') break;
#endif
/* Did the user pass an explicit VM type? */
i = KnownVMIndex(arg);
if (i >= 0) {
jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
isVMType = 1;
*pargc = *pargc - 1;
}
/* Did the user specify an "alternate" VM? */
else if (strncmp(arg, "-XXaltjvm=", 10) == 0 || strncmp(arg, "-J-XXaltjvm=", 12) == 0) {
isVMType = 1;
jvmtype = arg+((arg[1]=='X')? 10 : 12);
jvmidx = -1;
}
if (!isVMType) {
newArgv[newArgvIdx++] = arg;
}
}
/* Finish copying the arguments if we aborted the above loop.
NOTE that if we aborted via "break" then we did NOT copy the
last argument above, and in addition argi will be less than
argc. */
while (argi < argc) {
newArgv[newArgvIdx++] = (*argv)[argi];
argi++;
}
/* argv is null-terminated */
newArgv[newArgvIdx] = 0;
/* Copy back argv */
*argv = newArgv;
*pargc = newArgvIdx;
/* use the default VM type if not specified (no alias processing) */
if (jvmtype == NULL) return knownVMs[0].name+1;
/* if using an alternate VM, no alieas processing */
if (jvmidx < 0) return jvmtype;
/* Resolve aliases first */
while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
if (nextIdx < 0) {
fprintf(stderr, "Error: Unable to resolve VM alias %s\n", knownVMs[jvmidx].alias);
exit(1);
}
jvmidx = nextIdx;
jvmtype = knownVMs[jvmidx].name+1;
}
switch (knownVMs[jvmidx].flag) {
case VM_WARN:
fprintf(stderr, "Warning: %s VM not supported; %s VM will be used\n",
jvmtype, knownVMs[0].name + 1);
jvmtype = knownVMs[jvmidx=0].name + 1;
/* fall through */
case VM_KNOWN:
break;
case VM_ERROR:
fprintf(stderr, "Error: %s VM not supported\n", jvmtype);
exit(1);
}
return jvmtype;
}
static void SetLibraryPath(char **original_argv, char *execname,
char *jrepath, char *_jvmpath)
{
#ifndef WIN32
char *arch = (char *)GetArch(); /* like sparc or sparcv9 */
char *oldpath = getenv("LD_LIBRARY_PATH");
/*
* We need to set LD_LIBRARY_PATH as follows:
*
* o $JVMPATH (directory portion only)
* o $JRE/lib/$ARCH
* o $JRE/../lib/$ARCH
*
* followed by the user's previous $LD_LIBRARY_PATH, if any
*/
char *jvmpath = strdup(_jvmpath);
char *newenv = malloc((oldpath?strlen(oldpath):0) +
2*strlen(jrepath) + 2*strlen(arch) +
strlen(jvmpath) + 52);
char *newpath = newenv + strlen("LD_LIBRARY_PATH=");
/* remove the name of the .so from the JVM path */
char *lastslash = strrchr(jvmpath, '/');
if (lastslash) *lastslash = '\0';
sprintf(newenv, "LD_LIBRARY_PATH="
"%s:"
"%s/lib/%s:"
"%s/../lib/%s",
jvmpath,
jrepath, arch,
jrepath, arch);
/* Check to make sure that the prefix of the current path is the
* desired environment variable setting. */
if (oldpath != 0 &&
strncmp(newpath, oldpath, strlen(newpath))==0 &&
(oldpath[strlen(newpath)] == 0 || oldpath[strlen(newpath)] == ':'))
return; /* already on the LD_LIBRARY_PATH */
/* Place the desired environment setting onto the prefix of
* LD_LIBRARY_PATH. Note that this prevents any possible infinite
* loop of execv() because we test for the prefix, above. */
if (oldpath != 0) {
strcat(newenv, ":");
strcat(newenv, oldpath);
}
putenv(newenv);
/* Unix systems document that they look at LD_LIBRARY_PATH
* only once at startup, so we have to re-exec the current executable
* to get the changed environment variable to have an effect. */
execv(execname, original_argv);
perror("execv()");
exit(1);
#endif
}
/*
* Adds a new VM option with the given given name and value.
*/
static void AddOption(char *str, void *info)
{
/*
* Expand options array if needed to accomodate at least one more
* VM option.
*/
if (numOptions >= maxOptions) {
if (options == 0) {
maxOptions = 4;
options = (JavaVMOption*)MemAlloc(maxOptions * sizeof(JavaVMOption));
} else {
JavaVMOption *tmp;
maxOptions *= 2;
tmp = (JavaVMOption*)MemAlloc(maxOptions * sizeof(JavaVMOption));
memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
free(options);
options = tmp;
}
}
options[numOptions].optionString = str;
options[numOptions++].extraInfo = info;
}
static void SetClassPath(char *s)
{
char *def = (char*)MemAlloc(strlen(s) + 40);
sprintf(def, "-Djava.class.path=%s", s);
AddOption(def, NULL);
}
/*
* Parses command line arguments.
*/
static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
char **pclassname, int *pret)
{
int argc = *pargc;
char **argv = *pargv;
jboolean jarflag = JNI_FALSE;
char *arg;
*pret = 1;
while ((arg = *argv) != 0 && *arg == '-')
{
argv++; --argc;
if(strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0)
{
if(argc < 1)
{
fprintf(stderr, "%s requires class path specification\n", arg);
PrintUsage();
return JNI_FALSE;
}
SetClassPath(*argv);
argv++; --argc;
}
else
if(strcmp(arg, "-jar") == 0)
{
jarflag = JNI_TRUE;
}
else
if(strcmp(arg, "-help") == 0 ||
strcmp(arg, "-h") == 0 ||
strcmp(arg, "-?") == 0)
{
PrintUsage();
*pret = 0;
return JNI_FALSE;
}
else
if(strcmp(arg, "-version") == 0)
{
printVersion = JNI_TRUE;
return JNI_TRUE;
}
else
if(strcmp(arg, "-showversion") == 0)
{
showVersion = JNI_TRUE;
}
else
if(strcmp(arg, "-X") == 0)
{
*pret = PrintXUsage();
return JNI_FALSE;
/*
* The following case provide backward compatibility with old-style
* command line options.
*/
}
else
if(strcmp(arg, "-fullversion") == 0)
{
fprintf(stderr, "%s full version \"%s\"\n", progname,
FULL_VERSION);
*pret = 0;
return JNI_FALSE;
}
else
if(strcmp(arg, "-verbosegc") == 0)
{
AddOption("-verbose:gc", NULL);
}
else
if(strcmp(arg, "-t") == 0)
{
AddOption("-Xt", NULL);
}
else
if(strcmp(arg, "-tm") == 0)
{
AddOption("-Xtm", NULL);
}
else
if(strcmp(arg, "-debug") == 0)
{
AddOption("-Xdebug", NULL);
}
else
if(strcmp(arg, "-noclassgc") == 0)
{
AddOption("-Xnoclassgc", NULL);
}
else
if (strcmp(arg, "-Xfuture") == 0)
{
AddOption("-Xverify:all", NULL);
}
else
if(strcmp(arg, "-verify") == 0)
{
AddOption("-Xverify:all", NULL);
}
else
if(strcmp(arg, "-verifyremote") == 0)
{
AddOption("-Xverify:remote", NULL);
}
else
if(strcmp(arg, "-noverify") == 0)
{
AddOption("-Xverify:none", NULL);
}
else
if(strncmp(arg, "-prof", 5) == 0)
{
char *p = arg + 5;
char *tmp = (char*)MemAlloc(strlen(arg) + 50);
if(*p)
{
sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
}
else
{
sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
}
AddOption(tmp, NULL);
}
else
if(strncmp(arg, "-ss", 3) == 0 ||
strncmp(arg, "-oss", 4) == 0 ||
strncmp(arg, "-ms", 3) == 0 ||
strncmp(arg, "-mx", 3) == 0)
{
char *tmp = (char*)MemAlloc(strlen(arg) + 6);
sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
AddOption(tmp, NULL);
}
else
if(strcmp(arg, "-checksource") == 0 ||
strcmp(arg, "-cs") == 0 ||
strcmp(arg, "-noasyncgc") == 0)
{
/* No longer supported */
fprintf(stderr,
"Warning: %s option is no longer supported.\n",
arg);
}
else
{
AddOption(arg, NULL);
}
}
if(--argc >= 0)
{
if (jarflag)
{
*pjarfile = *argv++;
*pclassname = 0;
}
else
{
*pjarfile = 0;
*pclassname = *argv++;
}
*pargc = argc;
*pargv = argv;
}
return JNI_TRUE;
}
/*
* Initializes the Java Virtual Machine. Also frees options array when
* finished.
*/
static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
{
JavaVMInitArgs args;
jint r;
memset(&args, 0, sizeof(args));
args.version = JNI_VERSION_1_2;
args.nOptions = numOptions;
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
if (debug) {
int i = 0;
printf("JavaVM args:\n ");
printf("version 0x%08lx, ", (long)args.version);
printf("ignoreUnrecognized is %s, ",
args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
printf("nOptions is %ld\n", (long)args.nOptions);
for (i = 0; i < numOptions; i++)
printf(" option[%2d] = '%s'\n",
i, args.options[i].optionString);
}
r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
free(options);
return r == JNI_OK;
}
#define NULL_CHECK0(e) if ((e) == 0) return 0
#define NULL_CHECK(e) if ((e) == 0) return
/*
* Returns a pointer to a block of at least 'size' bytes of memory.
* Prints error message and exits if the memory could not be allocated.
*/
static void *MemAlloc(size_t size)
{
void *p = malloc(size);
if (p == 0) {
perror("malloc");
exit(1);
}
return p;
}
static int isUTF16;
static void InitEncodingFlag(JNIEnv *env)
{
jclass system;
jmethodID getProperty;
jstring fileEncoding;
jstring value;
const char* str;
system = env->FindClass("java/lang/System");
getProperty =
env->GetStaticMethodID(system, "getProperty",
"(Ljava/lang/String;)Ljava/lang/String;");
fileEncoding = env->NewStringUTF("file.encoding");
value =
(_jstring*)env->CallStaticObjectMethod(system, getProperty, fileEncoding);
if (value == NULL) {
isUTF16 = JNI_FALSE;
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -