C++ java 启动器

发布时间 2023-06-04 15:18:59作者: 月渊
#include <Windows.h>
#include <stdlib.h>
#include <string>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
    // 设置环境变量 java_home
    std::wstring java_home = L"D:\\Tools\\jdk\\Java_11_win";
    _wputenv_s(L"java_home", java_home.c_str());

    // 设置环境变量 classpath
    //std::wstring classpath = L".;C:\\myapp\\lib\\*";
    //std::wstring env_classpath = L"classpath=" + classpath;
    //_wputenv_s(L"classpath", env_classpath.c_str());

    std::wstring java_exe = java_home+L"\\bin\\java.exe";
    std::wstring jar = L"abc.jar";
    std::wstring command = java_exe + L" -jar " + jar;


    STARTUPINFOW si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    CreateProcessW(NULL, (wchar_t*)command.c_str(), NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi);

    return 0;
}

//int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
//原因:VS工程属性配置问题,【链接器】->【系统】->【子系统】设置成了控制台
//解决方案:【连接器】->【系统】->【子系统】修改为窗口
//右击资源文件->添加->资源->Icon->导入-> logo.ico