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;
?>
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.
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.