3
votes

I'm writing a windows application using Qt (4.6.1) that uses QProcess class to execute a java application.

Here's basically the code:

process = new QProcess(this);
connect( process, SIGNAL( started() ),                  this, SLOT( onProcessStarts() ) );
connect( process, SIGNAL( finished(int) ),              this, SLOT( onProcessEnds(int) ) );
connect( process, SIGNAL( readyReadStandardOutput() ),  this, SLOT( onProcessOutputs() ) );
connect( process, SIGNAL( error(QProcess::ProcessError)), this, SLOT(onProcessError(QProcess::ProcessError)));

QStringList arguments;
arguments << "-jar";
arguments << "absolute_path\app.jar";   //the java app that I want to execute
arguments << "-blah-blah";              //some java app's arguments
process->start( "java", arguments );

This is how I start the java application, and it works ok BUT, as far as I tested only in my Windows XP machine. When I tested this on another computer with Windows 7, it failed.

In Windows 7, the QProcess signal error(QProcess::ProcessError) is emitted after process->start(...) giving me the error QProcess::FailedToStart

Also I tested this: QStringList arguments; arguments << "/c"; arguments << "java"; arguments << "-jar"; arguments << "absolute_path\app.jar"; //the java app that I want to execute arguments << "-blah-blah"; //some java app's arguments process->start( "cmd.exe", arguments ); But then cmd.exe complains not finding java...

I suspect there's some permission issue involved; I set my executable to be run as administrator, but no luck, so I have run out of ideas...

Obivously, java is installed in Windows 7 machine (calling it manually from cmd.exe works).

2
post this to [email protected] - SunnyShah
looks like you're environment is not configured. change your PATH environment variable to include the path holding java.exe for me it is "C:\Java\JRE\Bin", locate java.exe on your system and change this path accordingly. - Muhammad Anjum Kaiser
PATH is properly configured, as I said in my question, calling java from command line works (calling it from any location) - Pherrymason
Hi, I encounter the same problem with QProcess and java. Did you find a solution to this? can you post a workaround? thanks - theosem
Nope, I didn't find any solution. I abandoned the project some time ago and don't know if newer versions of Qt solved this. - Pherrymason

2 Answers

1
votes

You might want to check the QProcess environment as mentioned in the docs. I have seen cases where the applications / QProcess's environment differs quite a bit from the logged in users environment, so when executing something from code it does not work but when executing the exact same command as a system user it works.

Try dumping to what QProcess thinks it's environment looks like and see what is there:

qDebug() << QProcess::environment();

Hope that will help you get it working.

0
votes

I know it's been a long time, but I just experienced the same problem. I was running a bash script that contained a java execution in a QProcess, and everything except the java output was captured by the readyRead.. signals and mapped functions.

The solution for me was to add the bash redirect 2>&1 to the jave line:

java -cp %(cpPath)s org.opensha.step.calc.STEP_main 2>&1

That worked for me.