CAll Us: USA +1 9176775553   Login

PHP-Java bridge on plesk

 

What is php-java bridge?

The php-java bridge is the php extension for using the java code in php files . You can access Java clasess etc from within your PHP code.

How to install php-java bridge on plesk centos  ?

If you want to install php-java bridge then you need to follow the steps.

For java :

Please check JAVA is installed or not on your server.

[root@server]# which java
/usr/bin/java
[root@server]# ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Mar 12 20:00 /usr/bin/java -> /etc/alternatives/java
[root@server]# ll /etc/alternatives/java
lrwxrwxrwx 1 root root 39 Mar 12 20:00 /etc/alternatives/java -> /usr/lib/jvm/jre-1.6.0-openjdk/bin/java

Means java is installed into the folder “/usr/lib/jvm/jre-1.6.0-openjdk” so export the java home

[root@server]# export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk

If java is not installed then you can installed it by using following commands

[root@server]# yum install java

or

you can install it manually by using following steps

Download j2sdk-1.4.2 from Sun Download center http://developers.sun.com/downloads/ Here I have used j2sdk-1_4_2_18-linux-i586-rpm.bin which will install j2sdk using RPMS and set the Path of JAVA_HOME automatically

[root@server]# chmod +x j2sdk-1_4_2_09-linux-i586.bin
[root@server]# ./j2sdk-1_4_2_09-linux-i586.bin

Now Check if Java is installed on the server using command java -version

[root@server]# java -version
java version “1.6.0_07?
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing

Installation of PHP / Java bridge

[root@server]# cd /usr/src
[root@server]# wget ftp://ftp.pbone.net/mirror/ftp.sourceforge.net/pub/sourceforge/p/project/ph/php-java-bridge/OldFiles/php-java-bridge-4.1.8-1.src.rpm
[root@server]# rpm2cpio php-java-bridge-4.1.8-1.src.rpm | cpio -ivd
[root@server]# tar xzf php-java-bridge_4.1.8.tar.gz
[root@server]# cd php-java-bridge-4.1.8
[root@server]# phpize
[root@server]# ./configure --with-java=$JAVA_HOME
[root@server]# make
[root@server]# make install

Add the following lines in php.ini file

vi /etc/php.ini

extension=java.so
[java]
java.java_home = "/usr/lib/jvm/java-1.6.0-openjdk/bin"
java.java = "/usr/lib/jvm/java-1.6.0-openjdk/bin/java"
java.class.path = "/usr/local/src/JavaBridge.jar"
; NOT SURE about the next line, for me works
java.library = "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre/lib/i386/server/libjvm.so"
java.library.path = "/usr/lib/php/modules"
java.log_level = 2

The check php-java-bridge is properly installed or not

[root@server]#  php -i | grep java
Output is as follows

java
java support => Enabled
java bridge => 4.1.8
java.java_home => /usr/lib/jvm/java-1.6.0-openjdk/bin
java.java => /usr/lib/jvm/java-1.6.0-openjdk/bin/java
java.log_file => <stderr>
java.log_level => 2
java.security_policy => Off
java command => JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk/bin LD_LIBRARY_PATH=/usr/lib/php/modules: /usr/lib/php/modules/RunJavaBridge /usr/lib/jvm/java-1.6.0-openjdk/bin/java -Djava.library.path=/usr/lib/php/modules -Djava.class.path=/usr/lib/php/modules/JavaBridge.jar -Djava.awt.headless=true -Dphp.java.bridge.base=/usr/lib/php/modules php.java.bridge.Standalone LOCAL:9268 2
java status => running
java server => 9268

For testing =

[root@server]#cd /var/www/vhosts/default/htdocs/
[root@server]# mkdir java
[root@server]# locate Java.inc
[root@server]# cp /usr/local/src/META-INF/java/Java.inc  /var/www/vhosts/default/htdocs/java

Then create file Java.php in /var/www/vhosts/default/htdocs/java and add the following code in it.

[root@server]# cd /var/www/vhosts/default/htdocs/java

[root@server]# vi Java.php

#========================================
<?php
require_once("java/Java.inc");
$session = java_session();
java_autoload();
$system = new Java('java.lang.System');
// créer une instance de la classe Java java.lang.System dans PHP
// accéder aux propriétés
echo 'Java version=' . $system->getProperty('java.version') . ' <br />';
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
echo 'OS=' . $system->getProperty('os.name') . ' ' .
$system->getProperty('os.version') . ' on ' .
$system->getProperty('os.arch') . '<br />';
// Exemple avec java.util.Date
$formater = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
echo $formater->format(new Java('java.util.Date'));
?>
#========================================

Then access the URL

http://server-ip-address/Java.php

If the URL shows the output of code then the PHP-Java bridge is properly installed on your server

If you are getting any error then please ask me on this thread

[root@server]#