Calling Java from PHP (on Windows XP)

Here are the steps to run Java classes from PHP

  1. Get JDK installed at your server, you can download either Java SE or Java EE from Download side of Sun website. (download Java SE if you are new, while Java EE for enterprise projects)
  2. Make sure your PHP installation folder has ‘extensions’ folder in it, it’s not available if you installed your PHP with Windows installer. If you don’t have it, download the zip package, get the ‘extensions’ folder out of zip file and paste it into PHP folder.
  3. Now you need to modify the content in php.ini (mostly available in C:\Windows)
    • search for ‘extension=php_java.dll’, uncomment it (by removing the semicolon (;))
    • go to [java] section, add these lines (or replace the commented lines with these lines):

      java.class.path = “c:\php\extensions\php_java.jar; C:\Program Files\Java\jdk1.6.0\bin; C:\Program Files\Java\jdk1.6.0\jre\lib”
      java.home = “C:\Program Files\Java\jdk1.6.0; C:\Program Files\Java\jdk1.6.0\jre\lib”
      java.library = C:\Program Files\Java\jdk1.6.0\jre\bin\server\jvm.dll
      java.library.path = “c:\php\extensions\; C:\Program Files\Java\jdk1.6.0\jre\lib”

    • Save your php.ini, restart your web server.
  4. Now create a PHP file, with the following code:

    $system = new Java(‘java.lang.System’);
    echo ‘Java version installed = ‘ . $system->getProperty(‘java.version’) . “<br />”;
    echo ‘Java vendor = ‘ . $system->getProperty(‘java.vendor’) . “<br />”;
    echo ‘Running on server = ‘ . $system->getProperty(‘os.name’) . “<br />”;

  5. Try to run the PHP script in your browser to test the connectivity.
    Calling Java methods from PHP

However now I still could not have the PHP call my own custom Java class, will update in near future when I got it run 😉

Reference: http://www.php.net/java

My environment: Windows XP with IIS, PHP 4.1.4, JDK 1.6.0

Leave a Reply