问题描述:

I have a php executable native binaries that came from some php server apps in play market, and I tried them on my tablet using a shell app.

I'm creating a new app and trying to run it and receive the output but it doesn't work.

I've a function that does copy the assets to the device. I also have some code to test that they are there properly

File file4 = new File(getFilesDir().getAbsolutePath()+"/php");

file4.setExecutable(true);

s+=file4.getName();

s+=file4.exists();

s+=file4.canExecute();

s+=file4.length();

it does say correct filename exists, is executable and correct file length.

Now, I try to run it it doesnt give me the php output.

I've tried php -v for version output, doesn't work.

I've tried php phpfile.php >> file.html and read the file, doesn't work.

I've tried with -f and also without -f but it doesn't work.

Lastly, I tried giving the parameters and working dir separately to .exec and still it gives no proper output

When I tried runtime.getruntime.exec with other commands like /bin/cmd/ls ... it gives a correct output.

The php binaries do work, I've tried 2 of them on my device, none work in my app.

I would appreciate help.

s+=runphp();

public String runphp(){

try {

String prog= "./php";

String[] env= { "-f", getFilesDir().getAbsolutePath()+"/phprun.php"}; // ">>","phpoutput.txt"

File dir= new File(getFilesDir().getAbsolutePath());

Process p = Runtime.getRuntime().exec(prog,env,dir);

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=null;

String output="";

while ((line = in.readLine()) != null) {

output += line;

}

in.close();

p.waitFor();

return output;

} catch (IOException e) {

throw new RuntimeException(e);

} catch (InterruptedException e) {

throw new RuntimeException(e);

}

}

The function does work for other commands.

网友答案:

The method Runtime.exec(String prog, String[] envp, File directory) requires you to give environment in the form name=value in the String[] envp.

But you are using it to pass arguments to your php program.

Use the following method instead,

Process exec (String[] progArray, String[] envp, File directory);

and pass all your command line arguments too in the first array itself.

Your command should look like:

String[] progArray= {"./php", "-f", getFilesDir().getAbsolutePath()+"/phprun.php"}; // ">>","phpoutput.txt"
File dir=  new File(getFilesDir().getAbsolutePath());
Process p = Runtime.getRuntime().exec(progArray,null,dir);
相关阅读:
Top