如何在没有控制台窗口的情况下从c++启动.jar文件

How to launch .jar file from C++ WITHOUT console window

本文关键字:情况下 c++ 启动 文件 jar 窗口 控制台      更新时间:2023-10-16

当我启动.jar文件时:

#include <windows.h>
using namespace std;
int main() {
    system("start javaw -splash:someImage.png -jar someFile.jar");
    return 0;
}

我还在g++编译器中定义了-mwindows选项。

闪屏前-黑色控制台显示几毫秒。有什么办法可以避免这种情况吗?

使用:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

有一个标志CREATE_NO_WINDOW,传递你的命令作为参数

在您的情况下,system()函数只是执行cmd.exestart ...参数。因此,黑色控制台窗口属于启动的cmd进程。javaw程序没有控制台窗口。用javaw -splash:someImage.png -jar someFile.jar命令行创建一个新进程就足够了,正如在创建进程页面中所描述的。

我当前基于ernesstas Gruodis和其他人的工作示例,没有警告

#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{   
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    char cmdline[] = " -jar some.jar";
    // Start the child process. 
    if( !CreateProcess( "javaw.exe",   // No module name (use command line)
        cmdline,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        CREATE_NO_WINDOW,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi)            // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).n", GetLastError() );
        return 1;
    }
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return 0;
}

使用MinGW编译

g++ test.cpp -o test.exe -Wl,--subsystem,windows

-Wl,--subsystem,windows表示没有控制台窗口

我现在的初步解决方案是:

#include <windows.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <tchar.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
    STARTUPINFO si;
    memset(&si, 0, sizeof (STARTUPINFO));
    si.cb = sizeof (STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = FALSE;
    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof (PROCESS_INFORMATION));
    // Start the child process.
    if (!CreateProcess("C:\Program Files\Java\jre7\bin\javaw.exe",
            " -jar install.jar", // Command line.
            NULL, // Process handle not inheritable.
            NULL, // Thread handle not inheritable.
            0, // Set handle inheritance to FALSE.
            CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
            NULL, // Use parent's environment block.
            NULL, // Use parent's starting directory.
            &si, // Pointer to STARTUPINFO structure.
            &pi)) // Pointer to PROCESS_INFORMATION structure.
    {
        printf("CreateProcess failed (%d).n", GetLastError());
        return 0;
    }
    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);
    // Close process and thread handles. 
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return 0;
}

但是我得到了警告:

main.cpp: In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)'Main.cpp:38:16:警告:已弃用从字符串常量到"LPSTR{也就是char*}"[-Wwrite-strings]π)//指向PROCESS_INFORMATION结构的指针。

怎么可能在没有警告的情况下正确地做这件事?

PS -如果有人想添加一个图标到exe文件 -这里是极好的说明如何在NetBeans IDE中做到这一点。只要投入资源。修改这些行:

#include "ids.h"  
IDI_ICON ICON "nice.ico"

在id .h文件中:

#define IDI_ICON  101