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;
?>
3
  • echo hello; should be echo "hello";. Commented Aug 13, 2013 at 0:05
  • This looks like a duplicate: stackoverflow.com/questions/614995/calling-php-from-java Commented Aug 13, 2013 at 0:11
  • @Jlewis071 - that question is itself closed as a duplicate, linked to a question that is closed as "not a real question" Commented Aug 13, 2013 at 0:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Java is running on the server. PHP is running on the server.
Yeah but different servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.