1

I want to call a java program and fetch it's output in stdout. I followed the suggestions in stackoverflow. But it doesn't work.

I have add the class file to my CLASSPATH. And I can execute the command in cmd correctly as follows:

enter image description here

In my PHP file I call this program by

exec("java Hello", $output);
print_r($output);

It yields nothing but:

Array()

What is the problem? How can I fix this?

ps: Hello is a demo program, actually the program I want to call is much more complicated which might take 2 or more seconds in my machine(i5 4G).

7
  • Try and see what passthru() gives back (instead of exec()). exec() might be selective sometimes to catch only returned values rather than printed content. Use escapeshellarg() to secure your script against hacks. Commented Apr 7, 2013 at 8:14
  • @Allendar passthru returns the return value? Anyway, the browser gives 1. Commented Apr 7, 2013 at 8:18
  • If the main() function in your JAVA program returns 1 (return 0 = no errors, 1 = somethings wrong ;)) it means there was an error. It might have something to do that PHP's commandline doesn't fetch your JAVA the same way as you do in cmd. Both are of course different settings-wise. Like cakil answered it might work to loop on the Array() you got back, as echo/print in PHP won't show the inners of an Array by default, like you might be used to in other languages. Commented Apr 7, 2013 at 8:22
  • @Allendar thanks, I checked the returned Array carefully to ensure that I didn't miss anything, but it IS empty. And passthru actually gives nothing intead of 1, I said 1 because I mistakenly set the class. Anyway, thank you. And if this won't work anyway, I can alternatively try to wrap this in jsp file or some other server-script language. I just want to make it on-line such that users can access the processed data yield by the java program. The worst case is that I have to translate the java program to PHP or other server-script language... Commented Apr 7, 2013 at 8:31
  • Try to just put return 0; in your JAVA class, at least to test if passthru() is not always giving back 1. It should read RAW-input, so it's really something with calling JAVA from the PHP script. Maybe (to test) try execute JAVA from it's absolute path too. The more you test, the more you will mostly get to know :) Commented Apr 7, 2013 at 8:42

4 Answers 4

2

I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/ It's quite easy to install and works very well.

Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)

http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download

The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.

If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.

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

Comments

1

Try this:

exec('java -cp .:/path/to/folder/of/your/file Hello 2>&1', $output);
print_r($output);

the 2>&1 need to display errors.

Comments

0

Well, it yields array right? so instead print_r($output) try print($output[0]), that outputting 'Hello World' on my console :D

4 Comments

Actually it yields empty array, cuz print_r() prints recursively what is in the array. And I did try print($output[0]), which also gives nothing.
Well, I'm curious about the content of your hello world program. Mine is simple just System.out.print("Hello World");, in main procedure
me too. And as you see in the figure above, it runs good in cmd, and gives the right result.
Hmm, it has same problem as you, and not solved yet: stackoverflow.com/questions/12448915/… maybe some kind of bug, or try with higher permission while executing php?
0

try pipe

$command = 'java Hello';
$descriptorspec = array(
    1 => array(
        'pipe', 'w'
    )
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
    exit("failed to create process");
}
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) === 0) {
    print_r($content);
}else{
    exit("failed to execute Hello");
}

2 Comments

The browser outputs "failed to execute Hello", but I do execute java Hello correctly in cmd.
what does proc_close($process) print?

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.