I have a website and want to be able to allow the user to run a Java file on the server from the website.
I want the user to click a button which will run the Java file on the server AND anything printed to standard-out by the Java program will be printed out on the website for the user to see.
How can this be done (call Java program from PHP and feed the standard out from the Java file back to the PHP website in real time)?
Update:
Thanks for the answers on how to run the Java program from PHP. However I also want to be able, as the Java program is printing to stdout where it will be printing out a lot of text as it is executing, to be able to print this out on the webpage so that the user can see what stage the Java program is in its execution.
How can this be done and does it require any additional AJAX or JavaScript or anything like that?
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.
Is the passthru function of any use?
http://www.php.net/manual/en/function.passthru.php
Related
I have an AutoHotKey script that sends keystrokes to a running Java program on my computer. How can the Java program tell that the keystrokes came from this specific script? I would imagine it has to do with Java Runtime and Process APIs.
There is no native way for Java to tell if the click has come from external source unless the external source notify the recipient application of itself through some hidden input.
Java runs in a virtual machine and the underlying platform is exposed in a limited way compared to native language as C / C++
If a robot/macro/script outside of the JVM is using lets say java REST API or SOAP it can write a header , or something different to report itself to the recipient program. If you have a desktop client you can do something similar by setting a hidden field or some other similar mechanism.
I have a PHP web application that should perform reasoning on an OWL (Ontology Web Language) document. I know of two reasoners, both of which are written in Java, but I have a standard Apache server at my disposal, so I can type in PHP, not in JSP.
Is there a way to execute Java code on server, like this: the PHP script sends data to the Java code (reasoner), then Java code performs the reasoning and returns data to the PHP script? Is there a way for one Java program to be perhaps somehow wrapped in something on the server, or whatever?
Thanx,
Martin
I think a good approach in your case would be to expose the reasoner via a SPARQL endpoint, and write your application in PHP against the SPARQL protocol.
Calling out to Java from PHP sounds like a hack, and seems brittle. If you base your application on the SPARQL protocol, you can get away with just HTTP, JSON, & XML libs, you don't need anything strictly RDF or OWL specific. Could simplify your implementation quite a bit.
Further, if you use SPARQL protocol, it isolates you from implementation specific details for whatever reasoner/database you're using. You can switch to a new reasoner and as long as it's exposed as a SPARQL endpoint, you don't need to change the code in your webapp.
None of that really gets around you having to run the reasoner on a machine, and most likely you'll need a servlet container, though some options can run stand-alone.
You need first to be able to run some Java code on your machine and you have to install a JVM on your server.
Then you could look at the following answer: calling java code from PHP . This should give you an idea how to call a Java program from PHP.
An alternative hack if you don't rely too heavily on the reasoning would be to open the ontology with Protege, classify it and then save it as such. Therefore you won't have to use any Java on the server side and you would just consume the data present in the ontology.
I would like to know if there is any way to communicate with a running console program (preferably running on Linux / Debian) via PHP. I am currently trying to create a webinterface for a little (existing) console Java program and I have no idea if there is any way I could do this. Could I "inject" a piece of code, lets say, a remote control module, and then use this to "remote-control" the script via PHP?
(It would be great if the existing .jar file wouldn't be changed / just injection, no reprogramming)
I am grateful for every piece of advice!
If the running program has no communication interface, then you can't communicate with it. If it does however, then the answer very much depends on how the program receives external input.
If the program contains a network listening thread (daemon), then you can communicate with it on the loopback interface using CURL or raw sockets from PHP.
Other ways of communicating with the program would be to share access to a file (PHP writes the file, Java reads it) or via a database.
The database would be the best option - it is thread safe and both PHP and Java have excellent MySQL support (Java via JDBC).
If, however you do not need to actually interface with the running program only merely need to start/stop/restart it, you can do this with the system() function in PHP.
If the Java program just runs and outputs to console, then you can do it easily enough, something like this:
$output = system( "java com.yourcompany.package.RunnableClass" );
print $output;
Assuming that the user who is running PHP has access to the Java binary of course, and that you have permission to access the JAR file.
Accessing a running program is a bit more difficult. Most programs will not have this built in by default (nor should they - giving access to random external processes in many cases is not desirable). If it does, though, you are in good shape. If it doesn't, and you can change the Java code, then you're good. If not, then you may be out of luck.
If that is the case, another good approach might be to see what resources the Java code is accessing, and how it is accessing them. Then you can write something similar in PHP. Obviously this is not ideal as you'll be re-inventing the wheel, but if you need to get to the data or whatever it is, and can't use any of the approaches above, it will work.
I have a java program that has a healthy Java API, but I want build a primitive interface between my java application and a php script as those are the requirements of my project.
My first attempt was to write a PHP script that ran an passthru function to run the jar. i.e.
passthru("java -jar myjarfile param1 param2 param3")
This worked but proved to be quite slow because the jar file had to be launched and executed etc.
My next attempt was to create a servlet on Tomcat7 and interface it with PHP by usin the curl() command. i.e.
curl(http://myserver/mywebapp/myservlet?p1=param1&p2=param2&p3=param3);
This had excellent performance , but the servlet was very unstable and crashed after about 5 minutes (i was loading the server with about 1 request every 10 seconds)
I come to Stack Overflow asking: am i doing this right? Is there a better way? How can I have my java program running in a jvm and interact with it using PHP?
Thanks
There is a world of difference between the Java method of handling things and the PHP method of handling things.
PHP basically runs every script from beginning to end for each request, which amounts to a very imperative programming technique. Java, on the other hand, typically handles stuff by modules that remain in memory for many more than one request. To integrate the two, you need to consider more than the "function calls", you need to consider how those two environments can be meshed cleanly.
Launching the java per PHP request is asking Java to behave like PHP. In other words, you are going to discard most of the best reasons to use Java by making it work like PHP. Instead, consider setting up a Tomcat (or something similar) instance and passing a request from one to the other. In other words, have the PHP make a web request to a Java environment, which handles things without complete buildup and teardown of the Java interpreter (which is how PHP handles things).
I'm assuming that because you attempted to use a JAR you can have the PHP and Java on the same machine. You may find this document on Java integration in PHP quite exciting. Note that I have never used it, I only know it exists. Be sure to read the introduction.
I have a Java app that takes a long time to be initialized (so I can't use a command-line like interface) and I need to pass text and receive the output of a Java method from Python. Is it possible to load the Java application, have it open all the time the Python script runs and use a method from that app?
I don't think the use of Python helps all that much over a command line (at least not a *nix command line), but the basic idea is to communicate over a socket or some similar mechanism. That means that the Java application will have to be wrapped in some code which opens a socket and waits for the python script to contact it. If you are most comfortable with python, you could look to implement that wrapper in Jython.
I've used JPype for something similar and it worked great.
JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both Virtual Machines.
If the java app is running you should also consider xml-rpc as it also works well.
Py4J is a way to call Java from a python script. Here is the project website: http://py4j.sourceforge.net/