How to call Java application from php script

by Prabakaran 2012-08-02 10:45:29

Create and test your Java desktop application.




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


      }

    }




    Create a manifest file,
    MANIFEST.MF
    , with the following content:




    Main-Class: HelloWorld







  2. 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






Add the PHP/Java Bridge library to your Java application





  1. Copy JavaBridge.jar 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. This time your application doesn't display anything and doesn't return.


  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. Open a new command shell and 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 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


For further information please read the INSTALL.STANDALONE documentation from the documentation download file.
1071
like
0
dislike
0
mail
flag

You must LOGIN to add comments