Blog : Java Functions
Java Functions
Table of Contents
java_last_exception_clear — Clear last Java exception
java_last_exception_get — Get last Java exception
add a note add a note
User Contributed Notes 26 notes
up
down
1 Anonymous ¶8 years ago
For those who want to run php with a recent tomcat version, you'll need the following patch:
http://wiki.apache.org/tomcat/UsingPhp
up
down
2 rabisultan at gmail dot com ¶8 years ago
If you get this error
Fatal error: Unable to create Java Virtual Machine in
it can be fixed by modifying your php.ini file, you will need to add in the path to the JRE's lib folder.
My php.ini file (php4/Apache2):
java.class.path= "e:\minerva\php\extensions\php_java.jar; c:\j2sdk1.4.2_08\jre\lib; C:\j2sdk1.4.2_08"
java.home = "c:\j2sdk1.4.2_08\bin; c:\j2sdk1.4.2_08\jre\lib"
java.library = "c:\j2sdk1.4.2_08\jre\bin\server\jvm.dll"
java.library.path= "e:\minerva\PHP\extensions; c:\j2sdk1.4.2_08\jre\lib"
up
down
0 Vikas Kumar ¶6 years ago
wamp5_1.7.2.exe+jdk1.5.0_10 integration error
extension=php_java.dll
[java]
java.home="C:\Program Files\Java\jdk1.5.0_10; C:\Program Files\Java\jdk1.5.0_10\lib"
java.class.path="C:\wamp\php\ext\JavaBridge.jar; C:\Program Files\Java\jdk1.5.0_10\lib"
java.library.path="C:\wamp\php\ext; C:\Program Files\Java\jdk1.5.0_10\lib"
java.library="C:\Program Files\Java\jdk1.5.0_10\jre\bin\server\jvm.dll"
up
down
0 meaton53 at hotmail dot com ¶8 years ago
I just wanted to post this for people that were having the same troubles that I was having. I am running OES SuSe Linux 9-1. I started this adventure because I needed to connect to a progress database via PHP. And I couldn't use ODBC drivers because the progress ODBC drivers were simply to old for unixODBC and iODBC (progress 9.1c). So I had to use JDBC with this wonderful PHP extension along with the PHP-Java Bridge found at:
http://sourceforge.net/projects/php-java-bridge
So Just for some notes, in the PHP.INI file I had to have:
extension=java.so
[java]
java.java_home=/usr/local/java
java.classpath=.:/usr/progress/dlc91c/java/progress.jar
:/usr/progress/dlc91c/java/jdbc.jar
java.libpath=.:/usr/progress/dlc91c/lib
:/usr/local/lib/php/extensions
And be careful because lots of sites tell you that the two varibles above are java.class.path, and java.library.path.
And also to be able to connect to a progress database from java in Linux you have to set your LD_ASSUME_KERNEL varible to 2.4.0 to do this simply add to you profile.local file:
export LD_ASSUME_KERNEL=2.4.0.
If you want to know why please email me and I will tell you.
up
down
0 m mokhtar at gmail dot com ¶8 years ago
Getting PHP JavaBridge to work with PHP5 on windows server:
====================================
1- Install Java J2EE 1.5 + JDK 1.4 (which includes application server/deploy tool/etc...)
2- download pecl-5.0.5-Win32.zip and php-java-bridge_2.0.8.zip, which will include
extra dll(s)
- unpack pecl pkg to your extensions folder, in PHP5 its ext.
- unpack java-Bridge to root php folder, in my case its simply C:\PHP
Note: the java-Bridge inculdes new versions of certain files like php_java.dll
so, it would be wise to rename your old files that came with PECL pkg for example
file_old, to rollback at anytime.
In order to deploy/test Java-Bridge .war onto your java application server follow these steps
http://cvs.sourceforge.net/viewcvs.py/php-java-bridge/php-java-bridge/
INSTALL.WINDOWS?view=markup
Note: move JavaBridge.jar to your extensions folder. and in test.php file that came
with Java-Brdige package change line java_require("test/arrayToString.jar");
to java_require("tests.php4/arrayToString.jar");
Add the following to your php.ini file and restart server:-
java.classpath = "location of JavaBridge.jar file...i.e. to your PHP extensions folder\
JavaBridge.jar,also any other extra java files that you'll be instanciating using your php script"
java.java_home = "location of jdk\bin"
java.libpath = "location of php_java.dll file...i.e. also to your PHP extensions folder"
happy integration
Cheers!!
up
down
0 stanley dot turnteen at gmail dot com ¶8 years ago
The php-java-bridge from sourceforge works great - it's stable and fast. I'm using it with the Lucene full text indexing package (http://lucene.apache.org/java/docs/index.html).
up
down
0 beoran at gmail dot com ¶8 years ago
The php-java-bridge mentioned below is indeed the way to go. It also works fine on a php 4.3.2 Linux web server. I think I can reccomend it.
up
down
0 Anonymous ¶8 years ago
JVM runs fine the first time, then broke....
The php4 java extension is simply broken, don't use it. I am
using the "PHP Java Bridge" for PHP5 with great success on WinNT:
http://php-java-bridge.sourceforge.net
up
down
0 jost2345 at yahoo dot de ¶9 years ago
The PHP/Java bridge that has been posted some time ago is now available on sourceforge.net. It contains several bugfixes and is meant to be used as a replacement for the expermental PHP4 java bridge.
http://sourceforge.net/projects/php-java-bridge/
So please don't send me private e-mails anymore to obtain the sourcecode for the bridge.
PHP/5 users may want to wait for the official PHP/Java bridge, which I expect to appear in a few years when java contains the appropriate hooks.
up
down
0 okapi at yahoo dot com ¶9 years ago
As mentioned prior, Zend is working to be the first of an official servlet / scripting integration. I've been testing their reference implementation that's available on the JSR 223 site and it's working pretty good. Some minor issues around the edges, but been working with one of their developers to fix it. So far, pretty good.
up
down
0 a9702466 at gmx dot net ¶10 years ago
The Java Servlet SAPI works fine in a non-threading
environment. If you want to use threads (normal case) you
have to do several things to get it work:
- get the patched files from
http://www.pelikan-it.com/download.html
- replace the sapi/servlet/servlet.java and
sapi/servlet/servlet.c file by the patched files
- build the php like this
./configure --with-tsrm-pthreads \
--with-servlet=/opt/resin --with-java=/opt/java \
--prefix=/opt/php-4.3.4
The primary problem is, that the request and response
objects are stored as global instance-variables of the servlet
which will be overwritten by other threads invoking the
servlet (remember: just on servlet-object for all threads!) and
all crashes. I changed the servlet to pass the request and
response objects to the java- and native-methods (where
required) to prevent the problem.
Once you have solved this problem it works fine and needs
unlike the java-extension just one jvm.
up
down
0 norman at junkonline dot com ¶10 years ago
This works fine on MacOS X (Panther). The one trick is, you need to symlink "java.so" in your php extensions directory to "libphp_java.jnilib".
Here's the relevant section from my php.ini file for anyone who needs it:
[Java]
java.class.path = "/usr/local/lib/java/php_java.jar"
java.home = "/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home"
java.library.path = "/usr/local/lib/php/extensions/no-debug-non-zts-20020429"
extension=java.so
You may have decided to put php_java.jar somewhere other than where I did.
For some reason, setting java.library in the ini file causes this to fail, so I left it out and things worked flawlessly without it. YMMV.
up
down
0 georgedaswani at hotmail dot com ¶10 years ago
I was able to compile java support in no problem and got it working sun jdk 1.4.2_03 (linux)
make sure JAVA_HOME is set
make sure LD_LIBRARY_PATH has
$JAVA_HOME/jre/lib/i386
on php.ini
[Java]
java.home = "/opt/j2sdk1.4.2_03"
java.class.path = "/opt/php/lib/php/php_java.jar:/opt/php/java-packages"
java.library.path = "/opt/php/lib/php/extensions/no-debug-zts-20020429"
extension_dir= "/opt/php/lib/php/extensions/no-debug-zts-20020429"
extension = java.so
It's import to note that APACHE 2 needs to be compiled using "PREFORK" instead of a thread based else the java stuff will work for a minute, then stop working with the following error
"PHP Fatal error: Unable to create Java Virtual Machine"
Seems that the extension is not thread safe.
up
down
0 golob a with tail gimb little spot org ¶10 years ago
This module allows PHP to interact with some interesting pieces of software - for one, it allows PHP to convert XML FO files into PDF/PS/... with the use of Apache FOP processor.
Here's an example:
$basedir = new Java("java.io.File", ".");
$outdir = new Java("java.io.File", "out");
$outdir->mkdirs();
$fofile = new Java("java.io.File", $basedir, "xml/fo/helloworld.fo");
$pdffile = new Java("java.io.File", $outdir, "ResultFO2PDF.pdf");
echo "Input: ".$fofile->toString()."\n";
echo "Output: ".$pdffile->toString()."\n";
$driver = new Java("org.apache.fop.apps.Driver");
$logger = new Java("org.apache.avalon.framework.logger.ConsoleLogger");
$driver->setLogger($logger);
$driver->setRenderer($driver->RENDER_PDF);
$out = new Java("java.io.FileOutputStream", $pdffile);
$driver->setOutputStream($out);
$in = new Java("java.io.FileInputStream", $fofile);
$driver->setInputSource(new Java("org.xml.sax.InputSource", $in));
$driver->run();
$in->close();
$out->close();
?>
up
down
0 raggha at hotmail dot com ¶11 years ago
Just wanted to make available the solution I found posted on
http://bugs.php.net/bug.php?id=18600
to solve the typical "Fatal error: Unable to Create Java Virtual Machine" trouble for Win32 users (thanks buddy):
[12 Dec 2002 9:19am] Alberto.Sarini@libero.it
Hi there,
I'm confirming C�dric's knowledge - java is running correctly from within PHP on Apache only when PHP is running as a CGI and not as a SAPI module. When running PHP as Apache SAPI Module after the restart Apache only fist access to php page using java is running OK, all next reloads of this page caused "Fatal error: Unable to create Java Virtual Machine
in ..." I think this could be still not solved bug.
Thanks
Alberto
BTW I'm running on:
OS:Win2000 Server SP2
Apache:1.3.27
PHP Version 4.3.0-dev (Build Date Dec 12 2002 10:14:28)
Java version=1.4.1_01
Java vendor=Sun Microsystems Inc.
up
down
0 neil at lowden dot net ¶11 years ago
** Success with W2K Server, PHP 4.3.0 as IIS5 SAPI module and J2SDK1.4.1_01 **
After a bunch of annoyingly spurious "access violation" and "Unable to create Java Virtual Machine" errors
I changed the php.ini setting for java.library from:
"C:\j2sdk1.4.1_01\jre\bin\client\jvm.dll"
to:
"C:\j2sdk1.4.1_01\jre\bin\server\jvm.dll"
All above examples now work perfectly incuding my own classes and those at
http://www.onlamp.com/pub/a/php/2001/06/14/php_jav.html (thanks to emilebosch above on 18-Oct-2001 07:19).
I'd really like to know the difference between 'client' and 'server' JVM versions if anyone can illuminate us.
Notes:
1. Manual install of PHP 4.3.0 to C:\php with the following relevant extracts from php.ini:
extension=php_java.dll
[Java]
java.class.path = "C:\php\extensions\php_java.jar;C:\java\packages\"
java.home = "C:\j2sdk1.4.1_01\bin"
java.library = "C:\j2sdk1.4.1_01\jre\bin\server\jvm.dll"
java.library.path = "C:\php\extensions"
2. Default installation of latest Java SDK from Sun
3. Win2K Server SP2 with all(?) patches
4. My own classes are placed in C:\java\packages\
Hope this helps some people.
Regards
-Neil
up
down
0 jason at buildernaut dot com ¶11 years ago
ON Linux / Apache --with-apxs
Page cannot be displayed - Apache crapping out.
I spent a great deal of time getting this to work - but hey - I'm a newbie.
One thing I did find was that I experienced an error I have not seen on this board or anywhere else in searches.
If you try to run a test java .php page and you get a page cannot be displayed error, try adding the follwing to your apache startup script
export LD_LIBRARY_PATH=/usr/lib/j2sdk1.3/jre/lib/i386/server
:/usr/lib/j2sdk1.3/jre/lib/i386
And of course change to paths that mean something to you.
up
down
0 gturner at newedgenetworks dot com ¶11 years ago
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!
up
down
0 venuti at sissa dot it ¶12 years ago
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:
http://archives.neohapsis.com/archives/php/2001-03/0040.html
(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.
up
down
0 not_contactable at yahoo dot com ¶12 years ago
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 http://www.php.net/downloads.php), and then unzip the "php-4.1.1-Win32.zip"(from http://www.php.net/downloads.php) 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 http://www.mysql.com/Downloads/Contrib/mm.mysql.jdbc-1.2c.tar.gz).
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.
up
down
0 ywliu at _spam_me_not_ dot hotmail dot com ¶12 years ago
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.
http://www.phpbuilder.com/annotate/message.php3?id=1009175
http://www.phpbuilder.com/annotate/message.php3?id=1008864
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
up
down
0 fernando at lozano dot eti dot br ¶12 years ago
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.
up
down
0 Magnus_Naeslund at nowhere dot to dot be dot found ¶12 years ago
An _very_ important thing here is to remember that the apache must be linked with pthreads (do "LDFLAGS=-lpthread ./configure ", 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
up
down
-1 pablo-at-godel.com.ar ¶11 years ago
For those using Redhat 7.x and Sun JDK, you have to set in LD_LIBRARY_PATH the directory where libjava.so .
In my case it looks like:
LD_LIBRARY_PATH=:/usr/local/j2sdk1.4.0_02/jre/lib/i386:\
/usr/local/j2sdk1.4.0_02/jre/lib/i386/server
This solved my problem with libjava.so not finding libverify.so
Hopes this helps to save some time for others.. I spent a lot of time before figuring this one out...
up
down
-1 alexg at syspro dot co dot za ¶11 years ago
According to an Article posted at:
http://www.zend.com/zend/week/week91.php#Heading7
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....
up
down
-1 amtd at 163 dot net ¶11 years ago
--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!