简体   繁体   中英

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

When I launch .jar file like:

#include <windows.h>

using namespace std;

int main() {

    system("start javaw -splash:someImage.png -jar someFile.jar");

    return 0;
}

Also I have defined -mwindows option in g++ compiler.

Before splash screen - a black console shows for a few millisecods. Is there any solution to avoid that?

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

There's a flag CREATE_NO_WINDOW , pass Your command as parameters.

In your case the system() function just executes the cmd.exe with start ... arguments. So the black console window belongs to the started cmd process. The javaw program has no console window. And it will be enough to create a new process with javaw -splash:someImage.png -jar someFile.jar command line as described on the Creating Processes page.

My current working example based on Ernestas Gruodis and others, with no warning

#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;
}

Compile with MinGW

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

-Wl,--subsystem,windows means no console window.

My preliminary solution now is:

#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;
}

But I get the warning:

main.cpp: In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)': main.cpp:38:16: warning: deprecated conversion from string constant to 'LPSTR {aka char*}' [-Wwrite-strings] &pi)) // Pointer to PROCESS_INFORMATION structure.

How it is possible to do it proper way without warnings?

PS - if someone wants to - here is superb instructions how to do that in NetBeans IDE. - 这里有一些精湛的说明如何在NetBeans IDE中执行此操作。 Just put in resource.rc these lines:

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

and in ids.h file:

#define IDI_ICON  101

That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM