home client api server api protocol f.a.q. downloads

Invoke Java standalone application methods.

This guide shows how to create a Java standalone application and how to call its Java procedures or methods from PHP scripts.

Create and test your Java desktop application.

  1. Create a simple application 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]);
      }
    }

    The manifest file, MANIFEST.MF, looks like this:

    Main-Class: HelloWorld


  2. Compile and run your Java application:

    javac -Djava.ext.dirs=. HelloWorld.java
    jar cvmf MANIFEST.MF HelloWorld.jar HelloWorld*.class
    java -jar HelloWorld.jar

Add the PHP/Java Bridge library to your Java application

  1. Copy JavaBridge.jar from the JavaBridgeTemplate.war zip file to the current directory.

  2. Add JavaBridge.jar to your Java application. Edit the manifest MANIFEST.MF like this:

    Main-Class: HelloWorld
    Class-Path: JavaBridge.jar

  3. 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]);
      }
    }

  4. Recompile your Java application.

  5. Run your Java application.

  6. 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"));
    ?>

  7. Run your test script. For example with:
    php -n -dallow_url_include=On test.php
  8. Your PHP script has called the method hello(), which should display a message dialog.
  9. Note that swing creates a non-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
For further information please read the INSTALL.STANDALONE documentation from the documentation download file.