2

I use Windows. I want to use QProcess in a running jar app (java/javaw). After starting that, open the cmd to show me information about working my jar app.

My problem is: when I exec my qt code, the Java app starts (in the background), but cmd doesn't open with the Java runtime.

My code:

QProcess process;
process.setWorkingDirectory("D:\\Programs\\Qt\\Units\\MyJavaProjects\\StackExp\\target");
process.setProgram("java.exe");
process.setArguments({"-jar","StackExp-1.0-SNAPSHOT.jar"});
process.start();
process.waitForFinished (-1);

if (!process.waitForStarted())
{
    qDebug() << "1: " +process.readAllStandardOutput();
    qDebug() << "2: " +process.readAllStandardError();
    qDebug() << "The process didnt start" << process.error();
}

My jar app built in maven and it has type: Java Application.

I use java.exe and javaw.exe, both runtimes don't open cmd.

How can I exec jar app with QProcess or otherwise, and at the same time open cmd for see information about working jar app?

costaparas
  • 5,047
  • 11
  • 16
  • 26
Evendie
  • 149
  • 6

1 Answers1

3

You can use system() command to run visible command prompt window. Replace you code with a oneliner:

system(QString("java.exe -jar D:\\Programs\\Qt\\Units\\MyJavaProjects\\StackExp\\target\\StackExp-1.0-SNAPSHOT.jar").toStdString().c_str());

If you see just a flashing of the window you can add timeout call after your java call as follows to figure out what went wrong with the java call:

system(QString("java.exe -jar D:\\Programs\\Qt\\Units\\MyJavaProjects\\StackExp\\target\\StackExp-1.0-SNAPSHOT.jar & TIMEOUT 5").toStdString().c_str());

Note that system() call blocks until it returns.

If you want to keep the handle to the process you need to use QProcess. You can run command prompt as follows but you don't see it as a separate console window but it runs in the background. You can see it in task manager as command prompt process though.

QProcess process;
process.setWorkingDirectory("D:\\Programs\\Qt\\Units\\MyJavaProjects\\StackExp\\target");
process.setProgram("cmd.exe");
process.setArguments({"/c", "java.exe -jar StackExp-1.0-SNAPSHOT.jar"});
process.start();
process.waitForFinished();
qDebug() << "1: " +process.readAllStandardOutput();
qDebug() << "2: " +process.readAllStandardError();

Update:

It seems you can spawn a foreground console window by setting custom flags to the process with QProcess::setCreateProcessArgumentModifier as follows:

process.setCreateProcessArgumentsModifier(
            [](QProcess::CreateProcessArguments *args) {
    args->flags |= CREATE_NEW_CONSOLE;
    args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
});

Tested with ping command:

#include <windows.h>


QProcess process;
process.setProgram("cmd.exe");
process.setArguments({"/c", "ping 127.0.0.1"});
process.setCreateProcessArgumentsModifier(
            [](QProcess::CreateProcessArguments *args) {
    args->flags |= CREATE_NEW_CONSOLE;
    args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
});
process.start();
process.waitForFinished();
talamaki
  • 5,324
  • 1
  • 27
  • 40
  • I use this code (Testedd with ping command) and have errors: 1) mainwindow.cpp:191: error: C2065: 'CREATE_NEW_CONSOLE': undeclared identifier 2) mainwindow.cpp:192: error: C2027: use of undefined type '_STARTUPINFOW' 3) mainwindow.cpp:192: error: C2227: left of '->dwFlags' must point to class/struct/union/generic type 4) mainwindow.cpp:192: error: C2065: 'STARTF_USESTDHANDLES': undeclared identifier – Evendie Feb 27 '21 at 10:45
  • What do you think about it? – Evendie Feb 27 '21 at 10:47
  • 1
    You are missing include. Add #include – talamaki Feb 28 '21 at 14:44