Search code examples
tomcatwampserverphp-java-bridge

java/php Bridge: Executing custom functions of tomcat app on wamp server (php)


As the title says I want to use classes and functions provided by a web app running on tomcat 7 in a php script running on a wamp server. Although I did find something related, I did not find anything step by step on how to execute custom java classes and functions in php. I did manage to make this work though:

    require_once $_SERVER['DOCUMENT_ROOT']."JavaBridge/java/Java.inc";
    $System = java("java.lang.System");
    echo $System->getProperties();

In the end I want to be able to do something like that:

    $abc= java("myproject.abc");

where abc is a custom class in myproject. Btw. I did look in the offical guide (http://php-java-bridge.sourceforge.net/pjb/webapp.php) but it either isn't helpful or I just don't get it. I also found that stackoverflow post (How to call custom java methods from PHP?), problem is, I don't really now what he means by

Now just copy your java jar file in tomcat /webapps/JavaBridge/WEB-INF/lib/ folder...

What java.jar files?

Thanks in advance.


Solution

  • With java.jar was meant that you bundle the .java/packages of your application whose fuctions/classes you want to use into a .jar.

    In Eclipse: Select the packages/files -> right click -> Export, Select JAR file -> Next -> Check "Export generated class files and resources" -> finish.

    Now all you have to do is copy that .jar file into the WEB-INF/lib folder of the JavaBridge application (if you don't have one already, just let tomcat deploy your JavaBridge.war), and restart the tomcat server. You can call your functions then like that:

        <?php
            define("JAVA_HOSTS", "localhost:8080"); ////fill in on whatever port you're server is running on so Java.inc knows with whom it should connect
            require_once $_SERVER['DOCUMENT_ROOT']."JavaBridge/java/Java.inc"; ////specify where on your apache server the Java.inc file is
            $object=new Java("test.classname");
            $object->Method();
        ?>