0

I am trying to run the following from java file.
I am trying to display the php version and later will change to run php files.

 Process p = Runtime.getRuntime().exec("cmd /C PHP/php.exe -v");
 BufferedReader in = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }

When I run the program, there is no error. but console is not displaying anything :(
Thank you.

2 Answers 2

4

You have to wait for the process to execute and you must separate the arguments :

Process p = Runtime.getRuntime().exec("cmd", "/C", "PHP/php.exe", "-v");
BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream()));
p.waitFor();
String line = null;
while ((line = in.readLine()) != null) {
     System.out.println(line);
}

BTW, any time you have a problem in running a process, it's a good idea to look also at the error stream.

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

5 Comments

Why would waiting for the process make any difference??? If there is any output from the php process it will be written before the process exits. If you wait before you read the output, you risk a pipeline deadlock.
@Ken - yea ... because this isn't the solution.
@StephenC - then what is wrong with my code :( .. any help will save my life
@StephenC exec executes the command in a separate process. When the following line is executed, the process hasn't yet run. That's why you must wait for the end of this execution if you need the result.
@dystroy - nonsense. If in.readLine() is called before the exec'd command starts, the read will just wait until the command starts and outputs something. Besides, if this was the problem the OP would report that his original version hung. In fact he just said it produced no output.
3

I suspect that the problem is one of the following:

  • php.exe is not running at all, for example, because PHP/php.exe should be PHP\php.exe or something.
  • the command is writing the version information to its "error" stream rather than its "output" stream.

Either way, you need to try reading from the error stream. You should either see the version information or an error message from cmd or php.exe.


I should also explain in detail why @dystroy's answer is wrong.

What exec does is to create a new external process to run the command. This process is connected up to the JVM so that when your application writes to the getOutput() stream, that goes to the external processes "standard input", and when the external process writes to its "standard output" and "standard error", the data can be read using the getInput() and getError() streams respectively.

The connections between the respective streams are implemented using "pipes" that are provided by the operating system.

The thing about a pipe is that it only has a limited buffering capacity. If one process is writing to one end and the other process is not reading, the writing process will eventually be forced to stop writing. Or more precisely, it will block in a write system call, waiting for the pipeline to drain.

@dystroy's answer said to do this on the Java side:

  1. Start the process.
  2. Wait for the process to complete
  3. Read the output from the process.

But if the external process writes lots of output, it won't be able to complete, because it will get blocked while writing to the pipe. Meanwhile the Java side is waiting for the external process to complete BEFORE it starts to read the data from the pipe.

Deadlock.

7 Comments

when I try to display directory of C, with String[] command2 = {"cmd", "/C", "dir"}; The out put is directory of C:\Windows how can I display directory of C and then directory of PHP file which is in C and then the php.exe??
@Ken - use the complete absolute pathname for the php executable.
@Ken - and also use the exec(String[]) method so that you don't run into problems with exec(String) splitting the command string incorrectly.
Hello, sorry to ask again... I can't even show directory of C Folder properly.. U said to use absolute path, and i write it, it doesn't work too.. C:\PHP\php.exe is what i used.. can u pls show me a way to do it? I really am lost :(
I don't understand why you are messing around with dir, but if you really want to list the contents of a directory then System.exec(new String[]{"cmd", "/c", "dir", "C:\\PHP"}); and read its stdout.
|

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.