|
|
XLVII. PHP / Java IntegrationBevezet�s
There are two possible ways to bridge PHP and Java: you can either
integrate PHP into a Java Servlet
environment, which is the more stable and efficient solution,
or integrate Java support into PHP. The former is provided by a SAPI
module that interfaces with the Servlet server, the latter 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.
Figyelem | Ez a kiterjeszt�s
K�S�RLETI JELLEGGEL M�K�DIK. Ez azt jelenti,
hogy minden itt dokument�lt m�k�d�s, bele�rtve a f�ggv�nyek
nev�t, m�k�d�s�t vagy b�rmi m�s, amit a kiterjeszt�s kapcs�n
le�rtunk megv�ltozhat egy k�s�bbi PHP kiad�sban minden
figyelmeztet�s n�lk�l. Ezt a kiterjeszt�st csak a saj�t
felel�ss�gedre haszn�ld! |
K�vetelm�nyek
You need a Java VM installed on your machine to use this extension.
Telep�t�s
Build instructions for this extension can be found in
php4/ext/java/README.
Fut�sidej� be�ll�t�sok
The php.ini settings involved in configuring the Java
extension are java.library.path and
java.class.path.
Er�forr�s t�pusokEz a kiterjeszt�s semmilyen er�forr�s
t�pust nem defini�l. El�re defini�lt �lland�kEz a kiterjeszt�s semmilyen konstans
�rt�ket nem defini�l. P�ld�k
P�lda 1. Java Example <?php
// 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 '.
$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");
print $formatter->format(new Java('java.util.Date'));
?> |
|
P�lda 2. AWT Example <?php
// 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->add('North', $button);
$frame->validate();
$frame->pack();
$frame->visible = True;
$thread = new Java('java.lang.Thread');
$thread->sleep(10000);
$frame->dispose();
?> |
|
Notes:
new Java() will create an instance of a class if
a suitable constructor is available. If no parameters are passed and
the default constructor is useful as it provides access to classes
like java.lang.System which expose most of their
functionallity through static methods.
Accessing a member of an instance will first look for bean properties
then public fields. In other words, print $date.time
will first attempt to be resolved as $date.getTime(),
then as $date.time.
Both static and instance members can be accessed on an object with
the same syntax. Furthermore, if the java object is of type
java.lang.Class, then static members of the class
(fields and methods) can be accessed.
Exceptions raised result in PHP warnings, and NULL results. The
warnings may be eliminated by prefixing the method call with an
"@" sign. The following APIs may be used to retrieve and reset
the last error:
Overload resolution is in general a hard problem given the
differences in types between the two languages. The PHP Java
extension employs a simple, but fairly effective, metric for
determining which overload is the best match.
Additionally, method names in PHP are not case sensitive, potentially
increasing the number of overloads to select from.
Once a method is selected, the parameters are cooerced if necessary,
possibly with a loss of data (example: double precision floating point
numbers will be converted to boolean).
In the tradition of PHP, arrays and hashtables may pretty much
be used interchangably. Note that hashtables in PHP may only be
indexed by integers or strings; and that arrays of primitive types
in Java can not be sparse. Also note that these constructs are
passed by value, so may be expensive in terms of memory and time.
Java Servlet SAPI
The Java Servlet SAPI builds upon the mechanism defined by the Java
extension to enable 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 be appreciated.
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.
User Contributed Notes PHP / Java Integration |
|
09-Oct-2001 05:16 |
|
Yes, in fact it works perfect.
You'll have to have the JDK installed,
PHP Java extension compiled (--with-java), and the apropriate [Java]
section settings in your php.ini first though.
It seems like the
majority of problems that users report about this on unix/linux platforms
has to do with getting those php.ini settings right, since they vary
depending on what brand of JRE you use.
Suggest searching the web
for "PHP Java integration php.ini" for more info.
|
|
[email protected]
18-Oct-2001 08:19 |
|
Hey ppl,
i got this link for a friend, you can get more information on
the PHP and JAVA topic here :-)
Warm
regards,
Emile Bosch
|
|
ard(at)[email protected]
18-Nov-2001 07:26 |
|
If you get the 'Couldn't create Virtual Machine' error without any further
explanation, you could try and add the following setting to Apache's
environment.
LD_LIBRARY_PATH=/usr/lib/j2sdk1.3/jre/lib/i386/classic:/usr/lib/j2sdk1.3/jre/lib/i386
(Running
Debian 2.2 / Apache 1.3.19 / PHP 4.0.6 / Blackdown JDK 1.3.1)
|
|
[email protected]
25-Dec-2001 04:52 |
|
An _very_ important thing here is to remember that the apache must be
linked with pthreads (do "LDFLAGS=-lpthread ./configure
<options>", or like me, add it the the .spec file).
You can
check if your apache is pthreaded with "ldd $(which httpd)" if
you like.
I experianced a lot of hangs/errors due to this problem,
it's a shame that it's not in the docs / README (i didn't find
it).
Magnus
|
|
ywliu@_spam_me_not_.hotmail.com
08-Jan-2002 10:41 |
|
Hi all, After struggling for some time and referring to numerous
confusing articles , I finally came up with a working Java section in
php.ini, for Linux (RedHat 6.2) with Apache 1.3.22 and Sun JDK
1.3.1.
[Java] java.class.path=/usr/local/lib/php/php_java.jar:/home/sinanet/www java.home
=
/usr/java java.library=libjava.so extension_dir=/usr/local/lib/php/extensions/no-debug-non-zts-20010901 extension=libphp_java.so
Please
substitute your document root for /home/sinanet/www , and your
extension_dir (it varies from version to version ) to libphp_java.so for
extension_dir here.
I have tried this configuration with php 4.0.6,
4.1.0, and 4.1.1. It works pretty well. And I think this may save some of
your time to look for a working [Java] section.
Yen-Wei Liu
|
|
11-Jan-2002 05:25 |
|
About the java configuration in php.ini, setting java.library to the
location of libjava.so, doesn't work for me. It only works when I set it
to the location of libjvm.so. I hope this helps.
Xavier
|
|
24-Jan-2002 01:13 |
|
I agree Xavier. My php.ini settings is: [java] java.class.path =
/usr/local/etc/php4/ext/java/php_java.jar java.home =
/usr/local/jdk1.3.1_02 java.library =
/usr/local/jdk1.3.1_02/jre/lib/i386/hotspot/libjvm.so extension_dir =
/usr/local/etc/php4/modules extension = libphp_java.so
When I
set java.library=libjava.so and java.library.path, it doesn't work. Maybe
this will help the later. BTW. I found the PHP4.1.0 and PHP4.1.1
cann't generate libphp_java.so, if it happen to you, you can use
PHP4.0.6.
Jonathan
|
|
[email protected]
24-Jan-2002 10:20 |
|
I managed to get it working with IBM Java SDK 1.3.0 but it was not easy.
Here's my php.init:
[Java] extension =
libphp_java.so java.library.path =
/usr/local/lib/php/extensions/no-debug-non-zts-20010901 java.class.path
= /usr/local/lib/php/php_java.jar java.home =
/opt/IBMJava2-13 java.library =
/opt/IBMJava2-13/jre/bin/classic/libjvm.so
Besides, I had to edit
the httpd init.d script to include the statement:
export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/IBMJava2-13/jre/bin:\ /opt/IBMJava2-13/jre/bin/classic
I
guess java.library.path should serve for this, but it didn't worked.
|
|
[email protected]
25-Jan-2002 10:09 |
|
But php 4.1.1 isn't creating any libphp_java.so file even after using
--with-java=/usr/java/j2sdk1.40 option. /usr/java/j2sdk1.4.0/bin is in
path.
|
|
ywliu@_spam_me_not_.hotmail.com
30-Jan-2002 06:13 |
|
Hello,
There are two more useful links on phpbuilder.com in the
PHP&JAVA discussion. They are related to the PHP and Java integration
on Unix/Linux.
My settings (see above) is like that in the 1st
link.
Moreover,
I have the path to java in my PATH env. variable, so MAYBE its why my
libjava.so works. I couldn't find the origin of libjava.so, but now looks
there are libjava.so and libjvm.so two options.
I built my php from
the source package, and libphp_java.so does exist, at least on my RH6.2
with Apache 1.3.19 and Sun JDK 1.3.1. libphp_java.so should be found
under php-4.1.1/ext/java/.libs .
If you want to find some more
discussion about PHP & Java integration, you may try
www.phpbuilder.com and look for php&java. It's a good place to start
with.
Yen-Wei Liu
|
|
[email protected]
08-Feb-2002 11:36 |
|
JDK1.4 RC is out, here are my Windows settings that make the Java extension
work:
extension=php_java.dll [Java] java.class.path =
c:\php\java\php_java.jar java.home = d:\jdk1.4\jre java.library =
d:\jdk1.4\jre\bin\server\jvm.dll
I have not tested (yet) whether
installing just the JRE works; for servlet support w/ Tomcat you do need
JDK since Tomcat needs tools.jar that is not part of JRE.
|
|
[email protected]
03-Mar-2002 06:16 |
|
Installation of PHP's Java module under Win32(Apache/IIS).
I install
php in "C:\php", firstly I install with
"php-4.1.1-installer.exe"(from ),
and then unzip the "php-4.1.1-Win32.zip"(from )
to the C:\PHP.(because all the extension dll files are from this zip file,
including php_java.dll)
I also install my Java v1.3 in
"C:\jdk1.3"
And My Program require JDBC connection of the
Java Program, so I did install also the JDBC driver for MySQL in
"c:\JDBC4mysql\mm.mysql.jdbc-1.2c"(from ).
About
the PHP.ini: 1> You need to set the
extension_dir="c:\php\extensions"(directory of all the
extensions dll files)
2> Add those lines at the end of
php.ini ====== [Java] extension=php_java.dll java.class.path="c:\php\java\php_java.jar;c:\wwwroot;
c:\JDBC4mysql\mm.mysql.jdbc-1.2c" java.home="c:\jdk1.3\bin" java.library="c:\jdk1.3\jre\bin\hotspot\jvm.dll" java.library.path="c:\php\extensions" ======
*
extension: it is the DLL you wish to load in, here we want to load the
java module. * java.class.path: By default, you need to include the
"C:\php\java\php_java.jar" for Java & PHP connection, AND
those directories that contain the Java class files you wish to run. Here,
my program Java classes are in "c:\wwwroot\", and ALSO the JDBC
driver classes in "c:\JDBC4mysql\mm.mysql.jdbc-1.2c", because my
Program need to import it. * java.home: the bin directory (directory of
java.exe) * java.library: jvm.dll is supposed to be under JDK's
jre\bin\hotspot, but sometimes it doesn't, try to Find(Start->Find)
jvm.dll where it is. * java.library.path: which is the same as the
extensions directory.
|
|
[email protected]
04-Mar-2002 01:23 |
|
Invoking java servlet from PHP.
This might look silly, but I found many
people on the net asking how to invoke a java servlet from php. Read these
notes:
(look
for the message about servlets).
Basically, it's like calling any
other URL, so you can use the "file" function. Read also the
online documentation about "file".
This solution is much
easier than many others I have found around the net.
|
|
[email protected]
13-Mar-2002 07:03 |
|
Again about how to call java servlets from php... the above method I
described works as long as you don't need to send long strings as
parameters and this because they are sent via GET method that cannot
handle long strings. So you need to send the parameters via POST, which is
not straightforward at all! Infact, you need to open a socket and write
php code that sends the call to the servlet as if it came from a form with
post method. Look at this url for more information:
|
|
[email protected]
29-Mar-2002 12:23 |
|
I have written a tutorial on compiling PHP with Java support, under Linux.
You can find it at www.thelinuxpimp.com:
|
|
[email protected]
15-Apr-2002 07:53 |
|
If you get the 'Couldn't create Virtual Machine' error, update your php to
version 4.1.1 (or greater) and the problem will be fixed.
|
|
[email protected]
22-Apr-2002 03:55 |
|
Answer to message from [email protected] 13-Feb-2002 12:26
"Java/php bridge? I used printwriter ... " Solution looks
like out.write("var1="+URLEncoder.encode(var1)); out.write("&var2="+URLEncoder.encode(var2)); out.write("&var3="+URLEncoder.encode(var3));
etc.
|
|
[email protected]
30-May-2002 08:20 |
|
Hello! I have two tips about getting PHP Java to work, hopefully google
will pick this up...
After you've followed the instructions
(building PHP with Java enabled and hacking php.ini) and you get a message
like:
Fatal error: Cannot instantiate non-existent class: java in
(file) on line (line)
That means PHP doesn't know what the 'Java'
function is. You can confirm that the Java extension isn't loaded by
looking at 'phpinfo' and verifying that there is no section titled Java.
To fix this you need to edit php.ini and set the 'extensions_dir' and an
'extension' appropriately. On our system it was 'extension_dir =
/opt/php/lib/php/extensions/no-debug-non-zts-20020429' and an 'extension =
java.so'. All the documentation I found said the extension library should
be called 'php_java.so', but our build produced 'java.so', I have no idea
why, YMMV.
The next error message I ran into was:
Fatal
error: java.lang.UnsatisfiedLinkError: no php_java in java.library.path in
(file) on line (line)
Yay! this confirms that the JVM is indeed
loading (UnsatisfiedLinkError is totally on the Java side), so relax,
you're almost done! What's happening is that the Java class
'net.php.reflect' (a class in php_java.jar that is built by PHP) is trying
to execute 'System.loadLibrary("php_java")' and the JVM is
unable to find the file named 'libphp_java.so' (or maybe 'php_java.dll' on
Windows?). If you ever heard of LD_LIBRARY_PATH, java.library.path is the
same thing. The solution has two parts: First making sure the php.ini has
a 'java.library.path' (in the '[java]' section) that's pointed at the same
directory that 'extension_dir', specifically the directory containing the
file 'java.so' (or maybe 'php_java.so' like everyone else). Second, this
part is bad IMHO, making a symbolic link from the ''java.so' file to
'libphp_java.so' so that Java's System.loadLibrary method can find what
it's looking for (apparently Java prefixes the filename it looks for with
"lib"). To make this symbolic link do 'cd' into the extensions
directory and run 'ln -s java.so libphp_java.so'.
Hope somebody
finds this useful!
|
|
[email protected]
09-Jun-2002 10:16 |
|
With latest PHP releases (4.2.1), compilation process doesn't produce a
valid name for the Java extension library: I have to rename it to
libphp_java.so in order to use it (initial name was java.so).
And
on my Debian box, I have to add the following lines in my apache init.d
script: export
LD_LIBRARY_PATH=/usr/local/java/jre/lib/i386 \:/usr/local/java/jre/lib/i386/classic/
It's
tricky but now it works well..
|
|
[email protected]
11-Jun-2002 10:45 |
|
--php.ini-------------------------
[Java] java.class.path =
.\extensions\php_java.jar java.home = c:\java java.library =
C:\java\jre\bin\server\jvm.dll java.library.path =
c:\apache\php\extensions
--system---------------------------
Windows
2000 P + Apache + SUN jdk 1.4.0-beta3
--app
path---------------------------
java(C:\java) Apache(C:\apache) PHP(C:\apache\php)
-----------------------------
success!
|
|
[email protected]
28-Aug-2002 06:50 |
|
According to an Article posted at:
there
might be some problems with getting php and jdk-1.4 to work toghether..
I`ve tried (php-4.2.2 and jdk-4.1) with no success....
|
|
|
| |