windows下检测java版本号的源代码

本文介绍了一种通过创建临时文件并使用Java命令来检测已安装Java运行时版本的方法。该程序能在Windows环境下运行,通过解析命令输出来获取Java版本号。
#include "stdafx.h"
#include <windows.h>
#include <string>
#include<fstream>
#include<iostream>
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
bool checkJavaRuntimeVersion() { 
    STARTUPINFOA 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";

    char buffer[MAX_PATH];


    char tempPathBuffer[MAX_PATH];
    GetTempPathA(MAX_PATH, tempPathBuffer);
    std::string file_name;
    if ( GetTempFileNameA(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) {
        MessageBoxA(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)) {
        MessageBoxA(NULL, "It appears that neither you have the Java Virtual Machine installed on your system nor its properly configured", "Launching", MB_ICONERROR | MB_OK);
        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
    DeleteFileA(file_name.c_str());
return true;
}  


int _tmain(int argc, _TCHAR* argv[])
{
checkJavaRuntimeVersion();
return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值