开发者

QProcess doesn't start java app under windows 7

开发者 https://www.devze.com 2022-12-21 20:30 出处:网络
I\'m writing a windows application using Qt (4.6.1) that uses QProcess class to execute a java application.

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开发者_如何学JAVA machine (calling it manually from cmd.exe works).


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消