tags:

views:

1350

answers:

4

I am writing a java class.And i have some logic in test.php One of the methods of that java class should invoke test.php so that test.php performs some computation and returns the result to the invoking java method. Can i do that with

runtime rt = runtime.getRuntime(); process exec = rt.exec(test.php);

Please suggest. Thanks in advance.

A: 

If you have a php cli installed, then you can do what you wrote here. Just run the php file with the CLI.

Itay Moav
+1  A: 

You may be able to execute the php script that way, but will it produce expected result?

Call to PHP done this way may work, but it will miss any context that it may need to work (parameters from a request, user's session scope, etc.)

david a.
A: 

This works in the opposite direction you are describing but it may be able to help as it is an actual bridge between the two languages.

http://www.php.net/manual/en/java.examples-basic.php

PHP has had a Java bridge for awhile now, but keep in mind it is experimental at best.

spligak
+3  A: 

You should use the JSR-223 scripting API. There is a PHP implementation that uses a Java/PHP bridge:

http://php-java-bridge.sourceforge.net/pjb/

Another is pure Java: http://www.caucho.com/resin-3.0/quercus/

I have not used this myself. But Caucho's Quercus product uses it, so I would suspect it is mature enough to do whatever you need.

http://quercus.caucho.com/

Here are some other links that may help you:

https://scripting.dev.java.net/

http://jcp.org/en/jsr/detail?id=223

joekutner