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.
Related
I'm curious to know if it's possible to fetch data from other applications to my Java program. I know java is running in a virtual environment, hence JVM and therefore have problems communicating with other applications unless you were to use the Robot class or so forth.
What I would like to do, for starters as educational purposes is to take let's say a music application like Spotify/iTunes, fetch the playlist (text data) and send/display it in a text file. I've tried a few things so far, and the only thing I've come close to is by using the Robot class, opening the application, doing Ctrl+A, pasting it in to a text document and so forth but that's more like a macro. I would like to make a java application that would do this automatically. Is that possible in any sense with Java or are you just better off changing languages? I wish to do it with Java though because it's the language I've studied for the past year and what I'm trying to master. (Sorry for the long explanation.)
It's nothing to do with Java and the JVM. Any language has the same problems and solutions for this sort of situation.
The thing you need to talk to has to provide you a way to talk to it. You need to talk to it using that way.
Methods include pipes, custom network protocols, SOAP and Restful web services, etc.
Just because an application runs in a virtual machine doesn't mean it cannot access external data through an API provided by an external program. For example, iTunes has a COM-based API for accessing playlists, and here is an example of using it from C#. You'll need something which allows accessing COM objects from Java.
(Please note I know nothing about this topic, this is just what I found with a little searching...)
You need to know which data interfaces provides the application you want to connect to.
As an example, maybe the application writes files to disk or stores some info in a database.
Then with Java you can read files, query the database, use an API (web services, REST, etc..) and so on...
I would like to know how to do to get variables of a C++ running program from my Java application. I think I have to do an API, but I don't know how to start this. In fact, I want to get information about packets in the Ekiga softphone. I localized what I want in Ekiga's main.cpp :
double lost = mw->priv->current_call->get_lost_packets();
double late = mw->priv->current_call->get_late_packets();
double out_of_order = mw->priv->current_call->get_out_of_order_packets();
I think what you need is JNI: http://en.wikipedia.org/wiki/Java_Native_Interface
You have to create an dll that will have methods that return this values and have a java class with native methods that will use this dll.
Sound like you want to implement a bridge layer using technologies such JNI or JNA.
A second option would be writing a Web service or Message passing layer between the two languages. I would avoid CORBA for such a simple problem (actually, I would avoid CORBA period hehehe).
Either that or have the C++ write the data to a Database, File, etc and write Java code to read it.
You can't directly access c++ variables from a different process. I do not know if ekiga already provides a way to get the data from external programs, so here are a few ways to get at the data (all involve modifying ekiga itself in some way).
Add a socket to listen for connections within the ekiga program and make your java program connect to this socket using client sockets. This way you can send the information to the connected java program wenever these values change.
Start the java Program within ekiga or make your java program start ekiga as a native method call. Both of these involve jni and result in both parts (ekiga and java program) running as a single process.
The first is simpler and less error prone, however you need some basic knowledge about network programming in both java and c++.
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.
This might be just too crazy to accomplish but this is what I'm trying to do:
I want an very basic java program that upon running will download another java program from a certain server and run that. I'm terrible with these kind of dynamic things but is there a way to download it and run it from inside the original program?
This is something I wanted to implement to prevent the need for issuing updates, assuming the computer must be connected to the internet in order to run the app, otherwise they can't.
Java does support this, and you can write this sort of thing yourself, but JNLP (aka Java WebStart), already does this, and it might require far less work on your part than rolling your own solution.
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