如何在没有控制台窗口的情况下从c++启动.jar文件
How to launch .jar file from C++ WITHOUT console window
当我启动.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.exe
与start ...
参数。因此,黑色控制台窗口属于启动的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
。
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 在C++中如何在没有pow的情况下进行基础计算
- 松弛原子与无同步情况下的记忆连贯性
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题