hotspot/src/os/windows/launcher/java_md.c
changeset 7452 b3fa838286de
parent 7410 f5b282e7a7a6
child 15228 e92acc84ade3
equal deleted inserted replaced
7450:e44c68293be8 7452:b3fa838286de
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
       
    25 #include <ctype.h>
    25 #include <windows.h>
    26 #include <windows.h>
    26 #include <io.h>
    27 #include <io.h>
    27 #include <process.h>
    28 #include <process.h>
    28 #include <stdlib.h>
    29 #include <stdlib.h>
    29 #include <stdio.h>
    30 #include <stdio.h>
   484     *cp = '\0';  /* remove the bin\ part */
   485     *cp = '\0';  /* remove the bin\ part */
   485     return JNI_TRUE;
   486     return JNI_TRUE;
   486 
   487 
   487 #else /* ifndef GAMMA */
   488 #else /* ifndef GAMMA */
   488 
   489 
   489     /* gamma launcher uses JAVA_HOME or ALT_JAVA_HOME environment variable to find JDK/JRE */
   490     char env[MAXPATHLEN + 1];
   490     char* java_home_var = getenv("ALT_JAVA_HOME");
   491 
   491     if (java_home_var == NULL) {
   492     /* gamma launcher uses ALT_JAVA_HOME environment variable or jdkpath.txt file to find JDK/JRE */
   492        java_home_var = getenv("JAVA_HOME");
   493 
   493     }
   494     if (getenv("ALT_JAVA_HOME") != NULL) {
   494     if (java_home_var == NULL) {
   495        snprintf(buf, bufsize, "%s", getenv("ALT_JAVA_HOME"));
   495        printf("JAVA_HOME or ALT_JAVA_HOME must point to a valid JDK/JRE to run gamma\n");
   496     }
   496        return JNI_FALSE;
   497     else {
   497     }
   498        char path[MAXPATHLEN + 1];
   498     snprintf(buf, bufsize, "%s", java_home_var);
   499        char* p;
       
   500        int len;
       
   501        FILE* fp;
       
   502 
       
   503        // find the path to the currect executable
       
   504        len = GetModuleFileName(NULL, path, MAXPATHLEN + 1);
       
   505        if (len == 0 || len > MAXPATHLEN) {
       
   506           printf("Could not get directory of current executable.");
       
   507           return JNI_FALSE;
       
   508        }
       
   509        // remove last path component ("hotspot.exe")
       
   510        p = strrchr(path, '\\');
       
   511        if (p == NULL) {
       
   512           printf("Could not parse directory of current executable.\n");
       
   513           return JNI_FALSE;
       
   514        }
       
   515        *p = '\0';
       
   516 
       
   517        // open jdkpath.txt and read JAVA_HOME from it
       
   518        if (strlen(path) + strlen("\\jdkpath.txt") + 1 >= MAXPATHLEN) {
       
   519           printf("Path too long: %s\n", path);
       
   520           return JNI_FALSE;
       
   521        }
       
   522        strcat(path, "\\jdkpath.txt");
       
   523        fp = fopen(path, "r");
       
   524        if (fp == NULL) {
       
   525           printf("Could not open file %s to get path to JDK.\n", path);
       
   526           return JNI_FALSE;
       
   527        }
       
   528 
       
   529        if (fgets(buf, bufsize, fp) == NULL) {
       
   530           printf("Could not read from file %s to get path to JDK.\n", path);
       
   531           fclose(fp);
       
   532           return JNI_FALSE;
       
   533        }
       
   534        // trim the buffer
       
   535        p = buf + strlen(buf) - 1;
       
   536        while(isspace(*p)) {
       
   537           *p = '\0';
       
   538           p--;
       
   539        }
       
   540        fclose(fp);
       
   541     }
       
   542 
       
   543     _snprintf(env, MAXPATHLEN, "JAVA_HOME=%s", buf);
       
   544     _putenv(env);
       
   545 
   499     return JNI_TRUE;
   546     return JNI_TRUE;
   500 #endif /* ifndef GAMMA */
   547 #endif /* ifndef GAMMA */
   501 }
   548 }
   502 
   549 
   503 #ifdef JAVAW
   550 #ifdef JAVAW