7

I cannot run any command via QProcess on Android platform. I am using Qt library. Can anyone explain how to run shell commands from my application on Android platform ?

    QProcess process();

    process.execute("ls");

   bool finished = process.waitForFinished(-1);

   qDebug() <<  "End : " << finished << " Output : " << process.errorString();

The process doesn't finish if I don't specify timeout. process.waitForFinished() returns false when I specify timeout let's say 10000 ms.

calynr
  • 1,264
  • 1
  • 11
  • 23

2 Answers2

5

Your example code is faulty and it will not work on ANY platform! ls command is not an exactable! This command is build into a shell program for example bash.

Another bug in your code is that QProcess::execute is a static function. So final line doesn't have impact on process you have tried to start.

So your code should look like this:

QProcess process;
process.start("bash", QStringList() << "-c" << "ls");

bool finished = process.waitForFinished(-1);
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I tried your code with process.start("bash", QStringList() << "-c" << "ls"); but it doesn't end. It runs on ubuntu without a problem but it doesn't run on android – calynr Jan 26 '17 at 14:25
  • 1
    question is: does Android has a `bash`? Check this with `adb`. And if it has it: do you have capability to run it (if it is needed)? Now you should take a look on logs and see what kind of error information is printed. – Marek R Jan 26 '17 at 14:55
  • `ls`is not a built in function, see bash documentation: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html – Benjamin T Jan 26 '17 at 16:03
  • I solved it, the problem was about breakpoints. When I run program without debugging mode it ran but now I need to run a binary file. It can be run from adb shell like ./ but I don't know how to run with QProcess – calynr Jan 27 '17 at 06:20
1

You are using QProcess::execute() which is a static function. Quoting Qt documentation: "Starts the program command in a new process, waits for it to finish".

So what could happen in your code is:

QProcess process();

process.execute("ls"); // Start "ls" and wait for it to finish
// "ls" has finished
bool finished = process.waitForFinished(-1); // Wait for the process to finish, but there is no process and you could get locked here forever...

There are 2 ways to fix your code:

QProcess process();

process.start("ls"); // Start "ls" and returns

bool finished = process.waitForFinished(-1);

qDebug() <<  "End : " << finished << " Output : " << process.errorString();

or

 QProcess::execute("ls");
Benjamin T
  • 8,120
  • 20
  • 37
  • Unfortunately, the result is same. It doesn't end, just waits – calynr Jan 26 '17 at 12:55
  • I've done some tests. Qt does not seem to lock waiting for a non existant process and the examples I gave you work fine on desktop and on Android. The problem can simply be that "ls" doesn't stop. I've seen cases like that on Debian where a process is accessing the file system and prevent other processes from accessing it, the other processes would the be stuck. – Benjamin T Jan 26 '17 at 13:21