Remotely calling Java from php - java

I have a java program that runs on a couple different computers and I want to be able to be able to access them remotely. I don't need to do anything crazy, just some very basic input output (get and set type stuff). I could write a client type application and set each program to a server with RMI and do it that way but I will not always be on the same computer and do not want to have to carry the .java file around with me.
I'd prefer to write a simple php webpage that I can access from anywhere and have some very basic buttons that could send the commands to the different Applications and receive back some output.
Problems is that it would probably impossible to get my host to load anything other then php onto the server where I'd host my webpage.
Does anyone have any ideas? I've tried googling it and found a couple things out there that sort of sound like what I need but the more I look into them they seem to not be what I wanted after all.

If you really want to do this right I'd suggest looking at a Java web framework (I use Play! myself) to create a RESTful web service and then sending requests from your PHP code that will fetch JSON data.
A framework like Play will make it very easy to get a REST web service running. Just create some wrappers that invoke your existing java code and call renderJSON to output the result.
From the PHP code you'll issue the request using curl and then read the response using json_decode which will turn it into a handy php variable.
This also has the advantage that either end can be changed without the other noticing, as long as the json data format is kept the same.

Based on the way you described your setup you'll probably run into routing issues trying to reach your Java apps from your PHP host. I'd recommend you try something else: have your Java apps frequently check with your PHP app if there are any updates. You can simply use a Java URL connection and a timer:
http://download.oracle.com/javase/6/docs/api/java/util/Timer.html
http://download.oracle.com/javase/6/docs/api/java/net/URL.html

Related

How can I utilize a Java program from PHP?

Say I have a php application that reads JSON stock data and stores it in MySQL.
Somewhere nearby on the server, I have a compiled Java (or Python, C, etc) executable that can take a set of numbers and return a set of analytics. Is there a way to call and receive data from that program from PHP without using an intermediary text file (or something similar?)
I see theres http://php-java-bridge.sourceforge.net/pjb/, but would it be necessary to run a Java server like Tomcat as well?
Technically you could use the exec function of PHP to call "java -jar YourExecutableJar.jar" and process the return value.
http://php.net/manual/en/function.exec.php
One "general" way is to connect them through sockets -> i.e have a listener at a port in Java that takes the set of numbers and returns the set of analytics back to the socket. The big advantage is that this is universal and can be done between any languages and between remote servers/clients as well.
Technically, any program runninng in background and accepting connections is server. Tomcat is only an example, used in numerous tutorial because it is relatively-light-and-easy-to-learn.
You can use Jetty: Shortest code to start embedded Jetty server
You can use JSON format as communication protocol (GSON library on Java side, on PHP side you already use JSON).

PHP get data or interact with a running Java process. (I think different than PHP/Java bridge)

I have an apache2 webserver with php installed and working. I am wondering if there is any way I can get a PHP script to access a Java program that is constantly running, mostly to just get information that the process keeps track of. The Java program is not a webapp or running within the apache server. It is a separate program.
Ive been looking into PHP/Java Bridge systems, and all they seem to do is let you run Java programs from PHP and vice-a-versa, which is not what I need. I can think of a way to do this via a web service and SOAP running on localhost, but if there is a way to avoid this and I can gain direct access to the Java program, I could get my webapp to run much faster.
The Java app that I am trying to "talk to" is a program that I wrote so I have access to the source code to make the necessary changes if something within the Java program needs to be set up.
Thanks in advance, I've been looking all over for something like this.
You'll need to communicate with the Java process in one way or another. As a web service is too heavy, you could simply communicate over a socket with a custom text or binary protocol.
Or you could also perhaps make the Java program write to a database every n seconds, and read from the database in your PHP script.
EDIT :
Look at http://www.php.net/manual/en/sockets.examples.php for a socket client example in PHP.
Look at http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html for a server socket example in Java.
PHP/Java Bridges let you call a running Java program from PHP.
Read: http://en.wikipedia.org/wiki/PHP/Java_Bridge
Read: http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php
The bridges actually use local socket communication, from the faq:
<?php
include_once("Java.inc");
$i1 = new Java("your.class", "1");
$i2 = new Java("your.class", "2");
$i3 = $i1->add($i2);
echo $i3->toString() . "\n";
?>
See Java.inc.
And edit your java code to contain the following line:
static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner.getInstance("9267");
Hey, so basically you need to run java application from php and get response from java application?
If its that so - I can't see any problem fro php/java-bridge. For example I've successfully run java encryption method for custom encryption in php.
You might look into Quercus/Resin. PHP runs inside Java on a Java web server. You can call Java code as if it was PHP functions/commands. All PHP commands are not supported, both most are. For example, you can run WordPress under Quercus.
http://www.caucho.com/resin-3.0/quercus/

Returning a value from a java web start application

Wondering if anyone knows if there is a way to "return" something from a java web start application into the code on the website. For example say the user needed to select a location in the java application. This would then pass the location value back to the code on the webpage (which is php and javascript). I have figured out how to pass arguments into a program, but so far cannot figure out any way to get them out after much googling. Any help would be much appreciated, thanks.
In principle no, since the Webstart application can be running without any Website open at all.
But if your clients use the Java-plugin from 1.6.0_10 or later (and not Safari and some other browsers with special Java-handling), you can use a JNLP-enabled applet, which is able to do the same things as a Webstart application (i.e. loading files and such), and is always bound to a webpage. It then can use the Javascript-bridge, or simply a loadDocument with the right parameters to feed back information.
You can use URL or sockets to connect back to the "same-origin" host. You can also use BasicService to open a web page, possibly from a different server, in a browser (although this shouldn't be used to send information back, as it'll be a GET not a POST).

From actionscript to google's datastore through java

I'm working on a flash game written in pure actionscript 3.0 in Flex.
I've just finished implementing replays for the game, but want to store the top 10 hiscores' replay data on my google-app-engine'd website.
I'm using Java for the app-engine stuff in Eclipse in java but I have no idea how to deal with communicating to my java code from my actionscript code.
I'll need to both read and write from actionscript -> java -> datastore. Does anyone have any experience with this?
For note, I'm horribly noob with anything to do with web development. I hear you can pass arguments to a URL when calling it, comparable to command-line arguments on a desktop executable and if so then sending all the data as a large string would be doable...
The question then would be how to call a url from AS3 code with additional data and then how to catch that on the java side.
Thanks to anyone who can help.
Jono
you can stuff the serialized replay into URLRequest::data and send it per POST.
You can also pass it as variables, creating a new URLVariables object, store that data with in it and pass it to URLRequest::data and send it either per POST or GET (the latter of which implies appending the parameters to the URL (whereas the former allows sending them in the request body)).
please note however, that there is no guarantee that GET parameters are properly supported if they exceed 2KB. also, requests using GET method should not have side effects. please see http request methods for further information.

Sending HTML Form Data to Java

I have a Java program that I'm trying to interact with over the web.
I need to gather form data from the user on a Drupal site that I don't have any control over, send it to the Java program, and send the output back to the user. The Java program needs to load a lot of libraries every time it's run, so it needs to be up waiting for data from the user.
It'd be best for me to just have an HTML form for the input. What's the simplest way to deal with HTML form data using Java?
Also, I'm trying to call the Java program from a shell script. I want the program running in the background though so the libraries are loaded in advance. So ideally, I could use the server I set up for both applications.
Thanks for any help.
It sounds like you really just want to write a servlet (or use a higher level web framework, but a servlet would work fine). That makes it very easy to get web form data - you just ask for values by name, basically.
You could then "script" the application using curl, wget or something similar to make requests to the servlet.
Apologies if this doesn't answer your question - I'm finding it slightly tricky to understand exactly what you're trying to do, particularly as there are multiple layers of web UI involved, as far as I can see.
The easiest way to make POST requests with java is to use the Apache HttpClient or the more recent HttpComponents libraries.

Categories

Resources