0

Here is my code:

String cmd = "bash -c \"php /Users/phyrrus9/Projects/java-web/test.php | say\"";
System.out.println("Executing: " + cmd);
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);

Yet, it is never executed. I can run that command from a shell and it works fine. Here is the contents of test.php

<?php
     echo hello;
?>
phyrrus9
  • 1,441
  • 11
  • 26

2 Answers2

1

Runtime.exec() is not a shell or a command interpreter, and is not treating your command the way you think it is. The single-String argument version of exec() does a simple brain-dead split on spaces, so your quotes and escaped quotes are meaningless; they are never making it to bash in the way you think.

Always always use one of the execs that take a String[] cmdarray

Your args in this case are

"bash"
"-c"
"\"php /Users/phyrrus9/Projects/java-web/test.php | say\""

That is, you are running bash -- the first arg you are giving it is -c and the second arg is the string.

Also see this answer to the more general question of how to Execute external program from Java.

Community
  • 1
  • 1
Stephen P
  • 14,422
  • 2
  • 43
  • 67
0

You cannot run it from a Process or Runtime class since PHP is a server side scripting which means it needs a server for it to run.

You need to use the HTTPDefaultClient class.

Take a look at this site it might help you.

Jed
  • 1,054
  • 1
  • 15
  • 34