2012-12-13 84 views

回答

6

如果你在Java

System.out.println(System.getProperty("java.version")); 
    System.out.println(System.getProperty("java.vendor")); 
    System.out.println(System.getProperty("java.vm.name")); 

輸出

1.7.0_03 
Oracle Corporation 
Java HotSpot(TM) Client VM 

有關於Java

for(Map.Entry e : System.getProperties().entrySet()) { 
     if (((String)e.getKey()).startsWith("java")) { 
      System.out.println(e); 
     } 
    } 

更多的道具。

java.runtime.name=Java(TM) SE Runtime Environment 
java.vm.version=22.1-b02 
java.vm.vendor=Oracle Corporation 
java.vendor.url=http://java.oracle.com/ 
java.vm.name=Java HotSpot(TM) Client VM 
java.vm.specification.name=Java Virtual Machine Specification 
java.runtime.version=1.7.0_03-b05 
.... 

至於其他的語言,我認爲你可以從你的應用程序運行%JAVA_HOME%/java -version,讀取輸出

java version "1.7.0_03" 
Java(TM) SE Runtime Environment (build 1.7.0_03-b05) 
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing) 

或者你可以用鼠標右鍵像上面一個JavaProps應用程序,運行它%JAVA_HOME%/java JavaProps做到這一點,讀取輸出

0

從C/C++檢測Java版本可能會非常棘手,問題是沒有可用於獲取已安裝的JRE屬性的標準API。一種方法是創建一個子進程,調用java -version並從此進程獲取輸出(也可以使子進程寫入特定文件,然後從中檢索數據),然後解析並獲取版本。

以下是我前一段時間做了一個Windows的exe程序啓動程序的修改版本,採用我剛纔解釋(創建子進程爲java -version,使子寫入到一個臨時文件,打開文件並閱讀其內容,解析版本):

void printJVMVersion() { 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 

    size_t sizeOfStartupInfo = sizeof(STARTUPINFO); 
    ZeroMemory(&si, sizeOfStartupInfo); 
    si.cb = sizeOfStartupInfo; 

    SECURITY_ATTRIBUTES sa = { sizeof(sa) }; // Open files in inheritable mode 
    sa.bInheritHandle = TRUE;     // Allow inheritance 
    sa.lpSecurityDescriptor = NULL;    // Handles to the child process 

    si.dwFlags = STARTF_USESTDHANDLES; 
    const std::string DEFAULT_TEMP_FILE_NAME = "temp_file.txt"; 
    const size_t MAX_PATH = 256; 
    char buffer[MAX_PATH]; 

    char tempPathBuffer[MAX_PATH]; 
    GetTempPath(MAX_PATH, tempPathBuffer); 
    std::string file_name; 
    if (GetTempFileName(tempPathBuffer, "some_random_prefix", 0, buffer) != 0) { 
     file_name = std::string(buffer); 
    } else { 
     file_name = DEFAULT_TEMP_FILE_NAME; 
    } 

    si.hStdError = CreateFileA(file_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, 0, NULL); 
    si.hStdOutput = CreateFileA(file_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, 0, NULL); 

    if (si.hStdOutput == NULL) { 
     MessageBox(NULL, "Error checking for the installed Java Virtual Machine, operation aborted", "Launching", MB_ICONERROR | MB_OK); 
     return false; 
    } 

    if (!CreateProcessA(NULL, "java -version", NULL, NULL, true, 0, NULL, NULL, &si, &pi)) { 
     MessageBox(NULL, "It appears that neither you have the Java Virtual Machine installed on your system nor its properly configured", "Launching", MB_ICONERROR | MB_OK); 
     ErrorExit("CreateProcessA"); 
     return false; 
    } 

    WaitForSingleObject(pi.hProcess, INFINITE); 

    if(si.hStdError) { 
     CloseHandle(si.hStdError); 
    } 
    if(si.hStdOutput) { 
     CloseHandle(si.hStdOutput); 
    } 

    // "Parse" the txt file 
    std::ifstream ifs(file_name.c_str()); 
    std::string line; 
    int versionIndex = 0; 
    int value[2]; 
    value[0] = value[1] = 0; 

    while (std::getline(ifs, line)) { 
     const std::string JAVA_VERSION_STRING = "java version "; 
     size_t index = line.find(JAVA_VERSION_STRING.c_str()); 
     if (index != std::string::npos) { 
      // get either the 1.X.X or 2.X.X 
      std::string version = line.substr(JAVA_VERSION_STRING.size()); 

      std::string::iterator ite = version.begin(); 
      std::string::iterator end = version.end(); 
      std::string tmp = ""; 
      for (; ite != end; ++ite) { 
       char c = *ite; 
       if (isdigit(c)) { 
        tmp += c; 
       } else if (c == '.') { 
        value[versionIndex] = atoi(tmp.c_str()); 
        versionIndex++; 
        tmp = ""; 
        // If we have, version, major and minor, then another set of digits is unnecessary 
        if (versionIndex == 2) 
         break; 
       } 
      } 
      std::cout << "detected java version: " << (value[0]) << ", " << (value[1]) << std::endl; 
     } 
    } 

    // Delete the temp file 
    DeleteFile(file_name.c_str()); 
} 

希望這能有所幫助

相關問題