I want to call Java from PHP 5.2, running either on a webserver or from a command line script.
From PHP 4, this seems to be simple, and just involves installing the PECL Java extension.
Example code from the PHP 4 extension:
<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
?>
However, this extension doesn't exist on PHP 5.
What is the closest alternative for PHP 5?
edit:
Really I'm looking for an interface similar to either a C PHP extension or to that provided by the PHP 4 Java extension. The Java program is fairly small and only needs to retain a small amount of state between calls and doesn't need to run asynchronously. The PHP script would only be running a small number of instances simultaneously.
This would also need to be deployed to multiple machines (running Ubuntu 9.x and Debian Lenny), so it should be simple to install.
This project seems to be a good bet: http://php-java-bridge.sourceforge.net/pjb/
Since you want to keep state at the java site I'd either use a java process that listens on a plain socket or use a simple embedded webserver (winstone or jetty) if you are more fluent writing servlets. Some other possibilities are listed at this related question: What is the best approach for IPC between Java and C++?
Now quite what you asked for, but you could have a look at Caucho's Resin, and in particular their Quercus which is a 100% Java implementation of PHP, it allows integration of Java and PHP.
I have a project that uses a Java program to fetch some data. It's quite simple, but I found that
$java_command="cd /var/java_dir && java my_java_program $myparam1 $myparam2";
$result=exec($java_command,$output,$return_code);
works just fine.
Zend Server also has a built-in daemon allowing you to access Java functionality from within PHP. It works right out of the box.
Related
I've been trying to excute a Python script from Java or Coldfusion framework (which runs on a JVM), The most documented solution is Jython but it only runs on Python 2.7 which is a problem because I need to use some librairies that runs only on Python 3 .
The JEP (https://github.com/ninia/jep) / JPY (https://github.com/bcdev/jpy) repositories on github really fits my needs but they are not quite responsives to the Github issues .
Has anyone ever done something similar before ?
Unless you really need to embed jython into your application, you may execute a system command (e.g. run a python script) from Java using the method described here.
The issue with Jep wasn't any syntax error, for example jep.eval(' some Python code'); was supposed to run that Python code in Java, If your system environement is well configured then what's left to do is to redirect use Jep's Python redirect_stream() function to redirect the flow of your IDE, worked for me finally .
As node.js still lacks important functionality which exists in Java, I would like to use Java instead of node.js, and create the client using a web language (html, js, css..).
Electron is cross platform and so does java so it seems fit to have a solution getting the best of both worlds.
Does someone know of a way to integrate electron with java or have a different solution to the problem?
I made something similar, Java back-end with Electron GUI.
You can do it in more ways, it depends on what you need.
You can create a jar file and then execute it like terminal:
https://nodejs.org/api/child_process.html
Or you can open a socket communication and talk on a Port. (A lot of documentation:
Java (web)socket - Node.js client.io)
In this second way, you can do everything you want, but you have to create your communication protocol.
Your path is not foolish, I am very satisfied of the communication and usage in my work with Java + Electron .
I've created a small PoC where Java process is integrated with Electron front-end: https://github.com/jreznot/electron-java-app There you will find a simple TODO List application built with Vaadin/Jetty and Electron.
Personally i made my back-end java communicate with the front-end by creating a file with te data then sending it to the main.js to be processed.
DBus and winDbus seems to be an option here.
It creates nice abstraction and separation between "frontend" and backend
https://sourceforge.net/projects/windbus/
I'm going to test it on my own soon
Is it possible to call a java pgm that is hosted on my ibmi from the pc?
I want to create a java program on my pc. The program should create a connection to my ibmi and call a java program hosted on my machine.
After searching in the web i try to find a solution in this forum.
is that possible?
what are the preferences that make that ?
edit
the java program on my ibmi should call a rpg/cobol program to grab some data and return them to the calling java on my pc.
bye jogi
Take a look at the JT400 project...
The IBM Toolbox for Java is a library of Java classes supporting the client/server and internet programming models to a system running IBM i (or i5/OS or OS/400). The classes can be used by Java applets, servlets, and applications to easily access IBM i data and resources.
Charles is right. JT400 is the way to go.
Here is a functional example.
Please provide some feedback if you run in trouble.
May I suggest looking at the answer to the following Stack Overflow post?
Accessing RPG on iSeries from Java
Depending on the situation, it might make more sense to call the RPG program directly from your PC's Java program. Otherwise, execute the CL Java command:
JAVA CLASS(my.java.class) PARM('a' 'b' 'c') CLASSPATH('/path/to/jarFile.jar') using the CommandCall class as described in the above mentioned post.
I have a problem relating my web project.
Previously when my professor ask me on doing a online judge application, I chose to implement it using php rather than Java servlet, as I thought java servlet quite complicated.
Then problem comes. For the online judge, I have to use a backend java program on the server to do the processing of user submitted codes. That is, each time a user submits something, php would call the java virtual machine and invokes the java application. However, to invoke the java application, my own way now is to use command line
popen("start java -jar \"$FILE_ROOT/OnlineJudge.jar\"", "r");
This works fine, but considering loading of java virtual machine, it is actually very slow and error prone. So I was wondering if there is any better ways for PHP to invoke local java programs on the server. Because later I found I still need to invoke more java from php.
Any idea would be appreciated. Thanks.
have you considered php-java bridge, this is quite useful, it uses XML to communicate between php and java.
You'll need to start the JVM once and the bridge component will help with communication.
here are some examples, here you can find a simple example to connect php and java
Depending on how the java application is written, it likely wouldn't be too hard to convert it to a servlet. Once it is a servlet, run it under jetty or tomcat, and then just have your php connect to it via CURL and put/get data from the servlet.
Java servlets are fairly simple. If you are using an external framework like Spring it becomes even easier, but if not, you just need to extend javax.servlet.http.HttpServlet, and then configure it in your web.xml.
It's a bit of a hack, but shouldn't take you too long to accomplish.
Also take a look at the exec for command line executions. I used it to shut down my computer after doing an extensively long task well into the night.
I have a PHP web application developed using symfony framework. I want to call some Java code from within my PHP pages. Java code is distributed to multiple jar files and there is one interface available to access the functionality. The input and outputs of the Java interface are XML files, text files, Java beans and some more complex objects.
Is there any bridge available in PHP to call complex Java methods or some other solution to this problem. Alternatively, I have to write soap web services to solve this problem.
You may want to look at these:
Zend Platform Java Bridge
PHP/Java Bridge
HTH
you could run the java code by exec or shell_exec
ex.
shell_exec("java whatever.jar $parameter1 $parameter2");
Webservices would be the nicer solution though. IMO