Want to run PHP and access Java? Here's how to do it on Windows XP using Apace and Apache Tomcat. Apache Tomcat is a Servlet container developed at the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server environment for Java code to run. Tomcat should not be confused with the Apache web server, which is a C implementation of a HTTP web server; these two HTTP web servers are not bundled together. Apache Tomcat includes tools for configuration and management, but can also be configured by editing configuration files that are normally XML-formatted.
Try it using XAMPP
Although the tutorial show you how to do things by installing Apache Tomcat, this may not be ideal or what most want to do. One of the things that stopped me from digging into Java web development is the fact that setting up an environment on Windows is just as hard as finding a cheap Java web hosting company. That and the fact that I like to be able to turn my development environments on and off. But now thanks to apachefriends.org half of my problem is solved. They have released an Apache Windows XAMPP [1]package with an easy to install Apache Tomcat add-on [2]. I have been waiting a long time for this to happen. This event should make things even easier for beginners.
- php-5.1.6-Win32.zip
- apache-tomcat-5.5.20.exe
- php-java-bridge_3.2.1_j2ee.zip [3]
- jdk-1_5_0_10-windows-i586-p.exe
- Windows XP Pro + SP2
Installation Directories
- C:\PHP
- C:\Program Files\Apache Group\Tomcat 5.5
- C:\Program Files\Java\jdk1.5.0_10
Java
- Make sure you set Environment Variables for Java (CLASSPATH and PATH). It looks like;
CLASSPATH = . JAVA_HOME = C:\Program Files\Java\jdk1.5.0_10 PATH = %PATH%;%JAVA_HOME%\bin
Apache Tomcat Server
- Run apache-tomcat-5.5.20.exe. Click Next >.
- Click I Agree.
- Select "Full" from the dropdown, and then click Next >.
- Click Next >. (in my case, C:\Program Files\Apache Group\Tomcat 5.5)
- Enter an admin password, and then click Next >.
- Verify the J2SE 5.0 JRE installation path is correct, and then click Install. (in my case, C:\Program Files\Java\jre1.5.0_10)
- click Finish.
- Open http://localhost:8080/ in your web browser. If you see something like the following, you have properly installed Tomcat!
- To install PHP as FastCGI, add these lines (from c:\php-java-bridge-3.2.1_j2ee\WEB-INF\web.xml)
to the end of your web.xml file (C:\Program Files\Apache Group\Tomcat 5.5\conf\web.xml):
... <!-- From c:\php-java-bridge-3.2.1_j2ee\WEB-INF\web.xml --> <!-- Faces Servlet --> <servlet> <servlet-name>PhpFacesServlet</servlet-name> <servlet-class>php.java.faces.FacesServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <!-- PHP Servlet: back-end for Apache or IIS --> <servlet> <servlet-name>PhpJavaServlet</servlet-name> <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class> <!-- Allow non-local clients. These clients connect to the --> <!-- back-end using a HTTP tunnel. Default is Off. --> <!-- Leave this off if your server is connected to the internet. --> <!-- <init-param> <param-name>allow_http_tunnel</param-name> <param-value>On</param-value> </init-param> --> <!-- Default it the java.log_level from the php.ini. --> <!-- See also the system property php.java.bridge.default_log_level --> <!-- <init-param> <param-name>servlet_log_level</param-name> <param-value>2</param-value> </init-param> --> <load-on-startup>0</load-on-startup> </servlet> <!-- PHP CGI servlet: when IIS or Apache are not available --> <servlet> <servlet-name>PhpCGIServlet</servlet-name> <servlet-class>php.java.servlet.PhpCGIServlet</servlet-class> <!-- Allow us to override the java.hosts, java.servlet and --> <!-- java.socketname settings from your php.ini so that we can --> <!-- use the current VM (see X_JAVABRIDGE_OVERRIDE_HOSTS). --> <!-- Default is On. --> <!-- <init-param> <param-name>override_hosts</param-name> <param-value>Off</param-value> </init-param> --> <!-- Your php binary. Default is /usr/bin/php-cgi or --> <!-- c:/php/php-cgi.exe --> <!-- <init-param> <param-name>php_exec</param-name> <param-value>php-cgi</param-value> </init-param> --> <!-- The number of concurrent cgi requests --> <!-- Note that this value should be 1/2 of the servlet engine's --> <!-- thread pool size because each PhpCGIServlet instance also --> <!-- consumes one PhpJavaServlet instance --> <!-- <init-param> <param-name>max_requests</param-name> <param-value>50</param-value> </init-param> --> <!-- Use the fast cgi interface instead of cgi when running --> <!-- php as a CGI sub component (when php is not installed as an --> <!-- Apache or IIS module). Values: Off, On, Autostart. --> <!-- Default is Autostart. --> <!-- <init-param> <param-name>use_fast_cgi</param-name> <param-value>Off</param-value> </init-param> --> <!-- Share the FastCGI pool from the JavaBridge PhpCGIServlet with --> <!-- the GlobalPhpCGIServlet used by all other web apps. --> <!-- Set this to On in both, the JavaBridge/WEB-INF/web.xml and --> <!-- in the global conf/web.xml. --> <!-- Default is Off. --> <!-- See http://php-java-bridge.sf.net#global-servlet for details. --> <init-param> <param-name>shared_fast_cgi_pool</param-name> <param-value>On</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <!-- Faces Servlet Mapping --> <servlet-mapping> <servlet-name>PhpFacesServlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <!-- PHP Servlet Mapping --> <servlet-mapping> <servlet-name>PhpJavaServlet</servlet-name> <url-pattern>*.phpjavabridge</url-pattern> </servlet-mapping> <!-- PHP CGI Servlet Mapping --> <servlet-mapping> <servlet-name>PhpCGIServlet</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping> <!-- Welcome files --> <welcome-file-list> <welcome-file>index.php</welcome-file> </welcome-file-list> ... </web-app> - Copy JavaBridge.jar and php-servlet.jar from C:\php-java-bridge-3.2.1_j2ee\WEB-INF\lib to C:\Program Files\Apache Group\Tomcat 5.5\shared\lib.
PHP 5 & Java Bridge
- You may use some sort of tools to unzip the php-java-bridge_3.2.1_j2ee.zip
file. I used ALZip. - Copy JavaBridge.jar from the JavaBridge.war to C:\PHP\ext.
- Copy java-x86-windows.dll from the JavaBridge.war to C:\PHP\ext.
- Rename java-x86-windows.dll (from C:\PHP\ext) to php_java.dll
- To set up a valid configuration file for PHP, make a copy of
php.ini-recommended then rename it to the php.ini.
- Edit the php.ini so that the
"extension_dir" points to your PHP 5 extension directory.... extension_dir = c:\php\ext extension = php_java.dll ...
- Add Java module settings to php.ini.
;;;;;;;;;;;;;;;;;;; ; Module Settings ; ;;;;;;;;;;;;;;;;;;; [java] java.java_home = "C:\Program Files\Java\jdk1.5.0_10\bin" java.java = "C:\Program Files\Java\jdk1.5.0_10\bin\javaw.exe" java.class.path = "C:\php\ext\JavaBridge.jar" java.library.path = "C:\php\ext" java.hosts = "127.0.0.1:8080" java.servlet = On java.log_level = 2
Test & Examples
- Open Notepad, and type (phpinfo.php)
<?php phpinfo(); ?>
- Save it as phpinfo.php into C:\Program Files\Apache Group\Tomcat 5.5\webapps\test.
- Open http://localhost:8080/test/phpinfo.php in your web browser.
- Example - java1.php
<?php // get instance of Java class java.lang.System in PHP $system = new Java('java.lang.System'); // demonstrate property access 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/>'; // 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')); ?> - Example - HelloWorld.java
public class HelloWorld { String hw = "Hello World"; public String getHelloWorld() { return hw; } } - To compile a java source file, type "javac HelloWorld.java"
- To create a JAR file from the class file, type "jar cvf HelloWorld.jar
HelloWorld.class". - Copy the HelloWorld.jar file into the C:\Program Files\Apache Group\Tomcat 5.5\webapps\test directory.
- Code the helloworld.php file.
<?php java_require('http://localhost:8080/test/HelloWorld.jar'); $myObj = new Java('HelloWorld'); // display Hello World echo (String) $myObj->getHelloWorld(); ?> - Open http://localhost:8080/test/helloworld.php in your web browser.
Sample Files
References
- PHP/Java Bridge - INSTALL.WINDOWS
Happy Publishing!





