4

我想查找计算机上安装的当前 Java 版本的版本号以及安装在任何具有 Flash 的给定 Web 浏览器上的 Flash 的当前版本。有没有办法使用 Java、C 或 C++ 来做到这一点?如果是这样,我应该研究什么课程/图书馆?

4

2 回答 2

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并读取输出

于 2012-12-13T02:04:54.090 回答
0

Detecting java version from C/C++ could be tricky, the thing is that there is not standard API you can use to get the installed JRE properties. A way to do it is creating a subprocess that invokes java -version and get the output from this process (also you can make the subprocess write to a specific file and then retrieve the data from it) , then parse it and get the version.

The following is a modified version of a Windows .exe launcher routine I did sometime ago that uses what I just explained (create the subprocess for java -version, make the subprocess write to a temp file, open the file and read its content, parse the 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());
}   

Hope this can be of any help

于 2012-12-13T03:29:09.577 回答