JavaFAQ Home » Good To Know
Look at Java PHP integration here! Examples included.
Good news for those who likes PHP as well
as Java! In Java 6, J2EE (Java Enterprise edition) will be able to work with PHP
and another scripting languages. Java and PHP will be intergated together. Java
classes will be accessible from different scripting engines, for example from
PHP. The Java classes will be a part of a Servlet application or will be in a
standard Java VM.
The JavaTM Servlet specification (Servlet 2.4, JSR-154, is the latest
version) defines the set of core abstractions that are used by Java developer
writing Web Applications. These abstractions include those of Web Application
context, session, request, response, etc.
When Java developers will write Web Applications, they will write classes
that interact with these objects in well defined security, resource and class
A Java API that used, will possibly through JNI interact with an scripting
language engine to access the desired Java objects. By now most of work is done
for PHP (Zend Technologies, Ltd, is a part of team, developing the feature ),
but it will be possible to work with script engine which will implement JSR 223.
It will be possible to bundle scripting pages into a WAR file, either
stand-alone, or as part of an EAR, providing real Java PHP integration.
You can ask - What is difference from today? It is already possible to do
Java PHP integration now... The difference is that it will be a part of Java
installation and you do not need custom solutions and make it manualy...
It is possible to do Java PHP integration, look here:
1. Bridge (integrate) PHP into Java: you can either integrate
PHP into a Java Servlet environment, which is the more stable and efficient
solution. This provided by a SAPI module that interfaces with the Servlet
The Java Servlet SAPI enables the entire PHP
processor to be run as a servlet. The primary advanatage of this from a PHP
perspective is that web servers which support servlets typically take great care
in pooling and reusing JVMs. Build instructions for the Servlet SAPI module can
be found in php4/sapi/README. Notes:
- While this code is intended to be able to run on any servlet engine, it
has only been tested on Apache's Jakarta/tomcat to date. Bug reports, success
stories and/or patches required to get this code to run on other engines would
- PHP has a habit of changing the working directory. sapi/servlet will
eventually change it back, but while PHP is running the servlet engine may not
be able to load any classes from the CLASSPATH which are specified using a
relative directory syntax, or find the work directory used for administration
and JSP compilation tasks.
2. Or bridge (integrate) Java support into PHP. This can be
done by this Java extension.
The Java extension provides a simple and effective means for creating and
invoking methods on Java objects from PHP. The JVM is created using JNI, and
everything runs in-process.
You need a Java VM installed on your machine to use this extension.
Build instructions for this extension can be found in
The php.ini settings involved in configuring the
Java extension are java.library.path and
Java PHP working Example 1. Use Java from PHP Example
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
print 'Java version='.$system->getProperty('java.version').' <br>';
print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';
print 'OS='.$system->getProperty('os.name').' '.
$system->getProperty('os.version').' on '.
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new Java('java.util.Date'));
PHP Java working Example 2. Use Java from PHP: AWT Example
// This example is only intented to be run as a CGI.
$frame = new Java('java.awt.Frame', 'PHP');
$button = new Java('java.awt.Button', 'Hello Java World!');
$frame->visible = True;
$thread = new Java('java.lang.Thread');
This tip is based on todays breaking news in Java and PHP world, php
documentation from php.net site (examples).
Printer Friendly Page
Send to a Friend
Search here again if you need more info!