0

I have a working exec("java $file") command in php, but the problem is that I'm not sure how to get standard output from a java program.

I realize that there is a second parameter (for example, exec("java $file", $output) ) but that doesn't seem to return output from a java program.

For example, if I have a java program with just a println("Hello World") in it, how can I get that output through exec() in php?

Thanks!

3 Answers 3

1

From the docs:

string exec ( string $command [, array &$output [, int &$return_var ]] )

If the output argument is present, then the specified array will be filled with every line of output from the command

Example:

exec('java ' . $file, $output);
print_r($output);
Sign up to request clarification or add additional context in comments.

Comments

1

nevermind. Okay, it works. You must run a print_r on the output to print the array, then you will see standard output.

1 Comment

print_r is just a print statement that works with arrays and objects as well as strings. It doesn't do anything to $output, it was always working.
0

You probably want to be using popen() or the even more general proc_open()

Comments

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.