If you are wondering how to access a Java function from your PHP code, this blog post is just for you. Answer to your problem is PHP/Java Bridge. You can do many more things using PHP/Java Bridge. Without beating around the bush let’s get started(happy-face).

What’s PHP/Java Bridge?

“The PHP/Java Bridge is an implementation of a streaming, XML-based network protocol, which can be used to connect a native script engine, for example PHP, Scheme or Python, with a Java virtual machine. It is up to 50 times faster than local RPC via SOAP, requires less resources on the web-server side. It is faster and more reliable than direct communication via the Java Native Interface, and it requires no additional components to invoke Java procedures from PHP or PHP procedures from Java.”  – Source 

pjb

Prerequisites

  • To be able to read the blog post. (tongue-out-face)
  • Working Linux system
  • Apache HTTP Server + PHP5
  • Apache Tomcat

1. Environment Setup

1.1 Download the PHP/Java Bridge

You can download the PHP/Java Bridge from this URL.

1.2 Install php-cgi

To use the PHP/Java Bridge you are going to need php-cgi on your system. So let’s install it.

sudo apt-get install php5-cgi

1.3. Let’s test the PHP/Java Bridge

Okay now let’s check whether the PHP/Java Bridge works fine. Extract the JavaBridge.war from the zip which you dowloaded. Navigate the directory where JavaBridge.war is located. To check run the below command.

java -classpath JavaBridge.war TestInstallation

Did you get the output below?

PHP/Java BridgeTest Installation

Hurrah!!! if so you are good to proceed to the next step.

2. Let’s deploy the PHP/Java Bridge on an Apache Tomcat

Put the JavaBridge.war file into the apache-tomcat-6.0.35/webapps folder. Now let’s try accessing the PHP/Java Bridge from the browser.

http://localhost:8080/JavaBridge

You should get the below output.

PHP/Java Bridge Works

If you do, don’t forget to check out the examples given. If you are done with the examples why stop here? Let’s move on.

3. Let the PHP/Java Bridge do it’s magic

Hang on! We’re almost there.

3.1 Configure the php.ini

To be able to use the PHP/Java Bridge you need to change your php.ini. By default allow_url_include is set to off. You need to set it to on. It tells whether to allow include/require to open URLs (like http:// or ftp://) as files. Find more info here.

allow_url_include = On

 

3.2 one last step

Create a new .php file on your Apache HTTP Server and put the code below.

<?php 
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$System = java("java.lang.System");
echo $System->getProperties();
?>

Now try accessing the PHP file from the browser. You should get the below out.

Output - PHP/Java Bridge

There is a lot more on PHP/Java Bridge. Let’s cover those topics in another blog post. Now you know the basics of the PHP/Java Bridge. Plus check out this video on PHP/Java Bridge installation.

If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).

6 Comments

  1. suchit kumar August 2, 2014 at 11:15 am

    Can you please describe the steps for adding the custom java classes without servelets to tomcat Because i have followed some steps to do that but i am getting classNotFoundException and the class is not getting loaded.My main objective is to call a pure java method which i have written in some other place i.e. it’s not the part of my Php project but for some reason i want to call this java method in php.I have installed tomcat and PhpJavaBridge and they are working fine.My OS is ubuntu 12.04.

    Reply
  2. Bala April 20, 2015 at 2:09 pm

    Great tutorial to start with.

    Reply
  3. Pingback: how to install php7-java bridge in ubuntu - ubuntutextbook

  4. Endang Djabidi October 30, 2016 at 3:39 am

    Hi
    my application use php java bridge
    its run on windows 7 or ubuntu
    but can not run on windows 10

    can you help me please..
    thank you

    Reply
  5. Regan Sharma April 12, 2017 at 7:24 am

    Hi,

    My problem is that i can not change php.ini to make changes available allow_url_include = On
    because this is security breach so please tell me other way to access this javabridge

    please help regarding this

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.