Installing PHP Java Bridge on Ubuntu

Source Install

First, make sure all php packages are updated — if you update PHP after installing this, it will likely segfault.

For the purpose of this, I’m using Ubuntu 7.10, and Java 6 (sun-java6)


First, get the build essential, as well as php5-dev, sun java and automake

# apt-get install build-essential php5-dev sun-java6-jre \
    sun-java6-jdk sun-java6-source automake

Second, get the PHP/Java bridge source.

# cd /usr/src
# wget http://internap.dl.sourceforge.net/sourceforge/php-java-bridge/php-java-bridge_5.2.0.tar.gz
# tar xzfv php-java-bridge_5.2.0.tar.gz

And, install.

# cd /usr/src/php-java-bridge-5.2.0
# phpize
# ./configure --with-java=/usr/lib/jvm/java-6-sun-1.6.0.03,/usr/lib/jvm/java-6-sun-1.6.0.03
# make
# chmod +x install.sh
# ./install.sh

Answering the questions for the install script is fairly simple. It looks for some tomcat stuff for the servlet portion of the Java Bridge, but we won’t be using it.

Copy *.inc to someplace it can be found.

# mkdir /usr/share/php/java
# cp *.inc /usr/share/php/java

Testing,

# php test.php

By default, java is only enabled in the command line php; to enable it in the global instances of php, edit the php.ini file and add the extension.

;; java.ini: Activate the PHP/Java extension

;; zend_extension = "/path/to/java.so"
extension = java.so

;; If you have installed the java-servlet.ini leave this file alone,
;; edit the servlet or standalone ini file.  Otherwise uncomment the
;; following java section and one of the following options:
[java]

There are no other options required. Make sure the java.hosts and java.servlet are disabled. If logging is desired, that’s another story.

16 Responses to Installing PHP Java Bridge on Ubuntu

  1. cshaffer says:

    Do the instructions for installing the PHP Java Bridge work for the latest version of Ubuntu (Herron)?

  2. cshaffer says:

    Hello,

    I am setting up the PHP Java bridge. Everything seemed to go OK, until I ran the php test.php. On examining the error output, it appears to be failing because port 8080 is not open. Is opening this port a requirement for the bridge or did I blow the install?

    Or could it be a version problem? The web server is running Herron.

  3. Hi, I believe if you comment out these two lines:

    ; java.hosts = 127.0.0.1:8080
    ; java.servlet = On ;; Off, On or User

    found in your php5/conf.d/java.ini file you should be ok.

    I haven’t officially tested my method with 8.04, but I’m assuming by your second comment it was successful.

    Hope that helps.

  4. steglasi says:

    I’ve successfully compiled it for Ubuntu 8.04. The only difference with the compile instructions above is the configure line:

    ./configure –with-java=/usr/lib/jvm/java-6-sun-1.6.0.03,/usr/lib/jvm/java-6-sun-1.6.0.03

    changes to:

    ./configure –with-java=/usr/lib/jvm/java-6-sun-1.6.0.06,/usr/lib/jvm/java-6-sun-1.6.0.06

    Apparently sun decided to bump their version number slightly for 8.04.

    Other than that, works perfectly!

  5. [...] long time ago I posted some instructions on installing PHP/Java Bridge on Ubuntu and that post is getting quite a few reads, so I figured I’d post a follow up on how it was [...]

  6. ks says:

    Working well! Thanks!

  7. Eduardo says:

    Hello, thx for the howto.

    I install the following this instructions but show me this error.

    http://172.17.0.12/java.php

    Fatal error: Class ‘Java’ not found in /var/www/java.php on line 4

    ############################################

    java.php

    getProperty(‘java.version’) . ”;
    echo ‘Java vendor=’ . $system->getProperty(‘java.vendor’) . ”;
    echo ‘OS=’ . $system->getProperty(‘os.name’) . ‘ ‘ .
    $system->getProperty(‘os.version’) . ‘ on ‘ .
    $system->getProperty(‘os.arch’) . ‘ ‘;

    // java.util.Date example
    $formatter = new Java(‘java.text.SimpleDateFormat’,
    “EEEE, MMMM dd, yyyy ‘at’ h:mm:ss a zzzz”);

    echo $formatter->format(new Java(‘java.util.Date’));

    ?>

    ############################################3

    Apache log.

    [Fri Jul 24 18:24:58 2009] [notice] caught SIGWINCH, shutting down gracefully
    Jul 24 18:25:09 VMBridge INFO : VM : 1.6.0_14@http://java.sun.com/
    Jul 24 18:25:09 VMBridge INFO : VMBridge version : 5.2.0
    Jul 24 18:25:09 VMBridge INFO : logFile :
    Jul 24 18:25:09 VMBridge INFO : default logLevel : 3
    Jul 24 18:25:09 VMBridge INFO : socket : INET_LOCAL:9267
    Jul 24 18:25:09 VMBridge INFO : java.ext.dirs : /usr/lib/jvm/java-6-sun-1.6.0.14/jre/lib/ext:/usr/java/packages/lib/ext
    Jul 24 18:25:09 VMBridge INFO : php.java.bridge.base: /usr/lib/php5/20060613+lfs
    Jul 24 18:25:09 VMBridge INFO : thread pool size : 20
    [Fri Jul 24 18:25:09 2009] [notice] Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch configured — resuming normal operations

  8. @Eduardo: make sure you’re including/requiring the Java.inc files that are with the java bridge source. They contain the source for the ‘Java’ class.

  9. Eduardo says:

    When i open:

    http://172.17.0.12:8080/JavaBridge/java/Java.inc

    “))) { $msg = ”
    PHP $version too old.
    \nFor PHP versions 0) { $thiz = $ar[$arLen-1]; return dirname($thiz); } else { return “java/”; } } $JAVA_BASE=java_get_base(); class java_RuntimeException extends Exception {}; class java_IllegalStateException extends java_RuntimeException {}; class java_IllegalArgumentException extends java_RuntimeException { function __construct($ob) { parent::__construct(“illegal argument: “.gettype($ob)); } }; function java_autoload_function($x) { $str=str_replace(“_”, “.”, $x); $client=__javaproxy_Client_getClient(); if(!($client->invokeMethod(0, “typeExists”, array($str)))) return false; $instance = “class ${x} extends Java {“. “static function type(\$sub=null){if(\$sub) \$sub=’\$’.\$sub; return java(‘${str}’.\”\$sub\”);}”. ‘function __construct() {$args = func_get_args();’. ‘array_unshift($args, ‘.”‘$str’”.’); parent::__construct($args);}}’; eval (“$instance”); return true; } function java_autoload($libs=null) { static $once = false; if($once) throw new java_IllegalStateException(“java_autoload called more than once”); $once = true; java_require($libs); if(function_exists(“spl_autoload_register”)) { spl_autoload_register(“java_autoload_function”); } else { function __autoload($x) { return java_autoload_function($x); } } } function Java($name) { static $classMap = array(); if(array_key_exists($name, $classMap)) return $classMap[$name]; return $classMap[$name]=new JavaClass($name); } function java_get_closure() {return java_closure_array(func_get_args());} function java_get_values($arg) { return java_values($arg); } function java_get_session() {return java_session_array(func_get_args());} function java_get_context() {return java_context(); } function java_get_server_name() { return java_server_name(); } function java_set_encoding($enc) { return java_set_file_encoding ($enc); } function java_set_library_path($arg) { return java_require($arg); } function java_defineHostFromInitialQuery($java_base) { if($java_base!=”java/”) { $url = parse_url($java_base); if(isset($url["scheme"]) && ($url["scheme"]==”http”)) { $host = $url["host"]; $port = $url["port"]; $path = $url["path"]; define (“JAVA_HOSTS”, “$host:$port”); $dir = dirname($path); define (“JAVA_SERVLET”, “$dir/JavaBridge.phpjavabridge”); return true; } } return false; } define (“JAVA_PEAR_VERSION”, “5.0.0″); if(!defined(“JAVA_SEND_SIZE”)) define(“JAVA_SEND_SIZE”, 8192); if(!defined(“JAVA_RECV_SIZE”)) define(“JAVA_RECV_SIZE”, 8192); if (defined (“JAVA_PARSE_INI_FILE”) && JAVA_PARSE_INI_FILE) { $ini=@parse_ini_file(“java.ini”); if(array_key_exists(“java.hosts”, $ini)) define(“JAVA_HOSTS”, $ini["java.hosts"]); if(array_key_exists(“java.servlet”, $ini)) define(“JAVA_SERVLET”, $ini["java.servlet"]); if(array_key_exists(“java.log_level”, $ini)) define(“JAVA_LOG_LEVEL”, $ini["java.log_level"]); } if(!defined(“JAVA_HOSTS”)) { if(!java_defineHostFromInitialQuery($JAVA_BASE)) { define(“JAVA_HOSTS”, “127.0.0.1:8080″); define(“JAVA_SERVLET”, “On”); } } else { if(!defined(“JAVA_SERVLET”)) define(“JAVA_SERVLET”, “On”); } if(!defined(“JAVA_LOG_LEVEL”)) define (“JAVA_LOG_LEVEL”, null); if(!defined(“JAVA_PIPE_DIR”)) { if (file_exists (“/dev/shm”)) define(“JAVA_PIPE_DIR”, “/dev/shm” ); elseif (file_exists (“/tmp”)) define(“JAVA_PIPE_DIR”, “/tmp” ); else define (“JAVA_PIPE_DIR”, null); } if (!defined(“JAVA_PREFER_VALUES”)) define(“JAVA_PREFER_VALUES”, 0); if(!defined(“JAVA_DEBUG”)) define(“JAVA_DEBUG”, false); class java_SimpleFactory { public $client; function java_SimpleFactory($client) { $this->client = $client; } function …..

    I dont understood because my php version is:

    root@ubuntu-dev2:/var/lib/tomcat5.5/webapps/JavaBridge/java# php -v
    PHP 5.2.4-2ubuntu5.6 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 17 2009 14:31:37)
    Copyright (c) 1997-2007 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
    root@ubuntu-dev2:/var/lib/tomcat5.5/webapps/JavaBridge/java#

  10. Java.inc is a PHP file that needs to be included in your PHP where you are trying to use Java.

    Trying to run Java.inc with your tomcat server won’t work.

    The file you are trying to access here:
    http://172.17.0.12/java.php

    needs to include the Java.inc file.
    require_once “/var/lib/tomcat5.5/webapps/JavaBridge/java/Java.inc”
    (assuming that is the correcet path.)

  11. eduardo says:

    Ok work but i need to make work in the :

    http://192.168.0.40:8080/JavaBridge/

    Always return this error:

    HTTP Status 404 – Servlet PhpCGIServlet is not available

    type Status report

    message Servlet PhpCGIServlet is not available

    description The requested resource (Servlet PhpCGIServlet is not available) is not available.
    Apache Tomcat/5.5

  12. Sorry, it looks like you’re trying to get PHP working on Apache Tomcat, and this thread is not about that.

    This is about getting Java support in regular Apache and PHP. All the references you’ve made to Tomcat are irrelevant to using the PHP/Java Bridge.

  13. james says:

    hi,

    im trying to make this work but i cant seem to find eh java folder which should be included in the source file.

    thanks
    james

  14. james says:

    hi again,

    im getting this error, though i have already commented the code on my java.ini

    Warning: pfsockopen() [function.pfsockopen]: unable to connect to 127.0.0.1:8080 (Connection refused) in /var/www/spotportal/jar/java/Java.inc on line 1034

    Fatal error: Uncaught exception ‘java_ConnectException’ with message ‘Could not connect to the J2EE server 127.0.0.1:8080. Please start it, for example with the command: “java -jar JavaBridge.jar SERVLET:8080 3 JavaBridge.log” or, if the back end has been compiled to native code, with “modules/java SERVLET:8080 3 JavaBridge.log”. Error message: Connection refused (111) ‘ in /var/www/spotportal/jar/java/Java.inc:1036 Stack trace: #0 /var/www/spotportal/jar/java/Java.inc(1042): java_HttpHandler->open() #1 /var/www/spotportal/jar/java/Java.inc(1157): java_HttpHandler->java_HttpHandler(Object(java_Protocol), ”, ‘127.0.0.1′, ‘8080′) #2 /var/www/spotportal/jar/java/Java.inc(1197): java_Protocol->createHttpHandler() #3 /var/www/spotportal/jar/java/Java.inc(1202): java_Protocol->createHandler() #4 /var/www/spotportal/jar/java/Java.inc(348): java_Protocol->java_Protocol(Object(java_Client)) #5 /var/www/spotportal/jar/java/Java.inc(1542): java_Client->java_Client() #6 /var/www/spotportal/jar/java/Java.inc(1873): __javaproxy_Client in /var/www/spotportal/jar/java/Java.inc on line 1036

  15. kunal says:

    Hi ,
    i followed ur steps mentioned but when i configure i get the following error and i tried almost everything in vain….this is driving me crazy… can you help me tell what the problem is?
    === configuring in server (/usr/src/php-java-bridge-5.2.0/server)
    configure: running /bin/bash ./configure –disable-option-checking ‘–prefix=/usr/local/zend’ ‘-with-java=/usr/lib/jvm/java-6-sun,/usr/lib/jvm/java-6-sun’ –cache-file=/dev/null –srcdir=.
    /bin/bash: ./configure: No such file or directory
    configure: error: ./configure failed for server

Leave a Reply

Name and Email Address are required fields. Your email will not be published or shared with third parties.