Invoke Java desktop application methods.
This guide shows how to create a Java desktop application and how to call its Java procedures or methods from PHP scripts.
Create and test your Java desktop application.
- Create a simple application,
HelloWorld.java
, which just displays a "hello world" dialog box:
import javax.swing.JOptionPane;
public class HelloWorld {
public static void main(String args[]) throws Exception {
JOptionPane.showMessageDialog(null, "hello world");
System.exit(0);
}
public void hello(String args[]) throws Exception {
JOptionPane.showMessageDialog(null, "hello " + args[0]);
}
}
MANIFEST.MF
, with the following content:
Main-Class: HelloWorld
- Compile and run your Java application. Open a command shell and type:
javac -Djava.ext.dirs=. HelloWorld.java
jar cvmf MANIFEST.MF HelloWorld.jar HelloWorld*.class
java -jar HelloWorld.jar
- Compile and run your Java application. Open a command shell and type:
Add the PHP/Java Bridge library to your Java application
- Copy JavaBridge.jar to the current directory.
- Add
JavaBridge.jar
to your Java application. Edit the manifestMANIFEST.MF
like this:
Main-Class: HelloWorld
Class-Path: JavaBridge.jar
- Open a communication port for PHP. Edit
HelloWorld.java
like this:
import javax.swing.JOptionPane;
public class HelloWorld {
public static final String JAVABRIDGE_PORT="8087";
static final php.java.bridge.JavaBridgeRunner runner =
php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT);
public static void main(String args[]) throws Exception {
runner.waitFor();
System.exit(0);
}
public void hello(String args[]) throws Exception {
JOptionPane.showMessageDialog(null, "hello " + args[0]);
}
}
- Recompile your Java application.
- Run your Java application. This time your application doesn't display anything and doesn't return.
- Create a PHP test script. For example:
<?php require_once("http://localhost:8087/JavaBridge/java/Java.inc");
$world = new java("HelloWorld");
echo $world->hello(array("from PHP"));
?>
- Open a new command shell and run your test script. For example with:
php -n -dallow_url_include=On test.php
- Your PHP script has called the method hello(), which should display a message dialog.
- Note that the above code doesn't create a application-modal dialog. The communication can be traced by starting your application with the options:
java -Dphp.java.bridge.default_log_file= -Dphp.java.bridge.default_log_level=4 -jar HelloWorld.jar
- Open a communication port for PHP. Edit