2

Which one is the best way to call a PHP script from JAVA? I'm calling now the PHP executable using the next code:

CommandLine command = new CommandLine("php");
command.addArgument("file.php");

DefaultExecutor exec = new DefaultExecutor();

PumpStreamHandler streamHandler = new PumpStreamHandler(null, null);
exec.setStreamHandler(streamHandler);

int execute = exec.execute(command);

But loading and disposing the php executable consumes resources. What about Java SAPI or something like loading a php module into java?

I'm calling the "exec.execute(command);" inside a loop.

Wiliam
  • 3,714
  • 7
  • 36
  • 56

4 Answers4

1

This seems to be a dup of various questions and answers, including:

Community
  • 1
  • 1
jtoberon
  • 8,706
  • 1
  • 35
  • 48
0

I've never tried it myself, but it seems there's actually PHP interpreter written in Java, so you can run PHP code straight from Java programs (sort of like JRuby)

http://www.caucho.com/resin-3.0/quercus/

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • Or maybe it's a good idea... but the only examples I have seen are about installing resin like a JAVA web server. I have to research more. – Wiliam Sep 05 '11 at 16:13
0

If you really want to use the native php on the system from java, you can try this PHP/Java Bridge. It claims to be faster and more efficient than just shelling out to php. It also appears to be able to call from Java to php or php to java in web and desktop applications. This Java to php example code might be helpful.

Jon7
  • 7,165
  • 2
  • 33
  • 39
0

By Java Servlet SAPI, you can enable the entire PHP processor to be run as a servlet.

xdazz
  • 158,678
  • 38
  • 247
  • 274