Run Java class file from PHP script on a website

The PHP exec() function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.

Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.

<?php exec("java -jar file.jar arguments", $output); ?>

Since you mention real time I would suggest setting up a PHP to Java Bridge. Initializing the JVM at each request takes up a lot of resources.

PHP/Java Bridge

The PHP/Java Bridge is an implementation of a streaming, XML-based network protocol, which can be used to connect a native script engine, for example PHP, Scheme or Python, with a Java or ECMA 335 virtual machine. It is up to 50 times faster than local RPC via SOAP, requires less resources on the web-server side. It is faster and more reliable than direct communication via the Java Native Interface, and it requires no additional components to invoke Java procedures from PHP or PHP procedures from Java.


I would rather wrap the Java class in a Java applet, which can then be invoked from a javascript call on the client side : see http://www.rgagnon.com/javadetails/java-0170.html

Otherwise, if the call throws a lot of text to the standard output or the class has to be run on the server because of system dependencies, calling from php exec is the way to go, but you will probably need something like cometd to display the text on the client in real time. There are implementations for various javascript toolkits such as Dojo or jQuery.

For the server side, there seems to be a cometd implementation in php here.

I hope this helps...

Philippe


Check out exec and the other program execution functions. But do this very carefully, or it's a recipe for exploits.