Thu 11 Sep 2003
I wanted to setup PHP to uses Java classes and had problems trying to get it to work. Here is the steps I took.
I already had Apache and PHP working with mysql from a previous article, so I am just going to concentrate on getting PHP compiled with Java.
I downloaded the Java standard edition SDK. I found other intructions just using the JRE( Java Runtime Enviroment) but php complains aobut not finding javac.
j2sdk-1_4_1_05-linux-i586.bin
copied to my /opt directoy
chmod a+x j2sdk-1_4_1_05-linux-i586.bin
run it and agree to the license.
./j2sdk-1_4_1_05-linux-i586.bin
then made a link
ln -s j2sdk1_4_1_05 java
I then created a new profile script in /etc/profile.d/
* then ‘^d at the end means hit the Ctrl key and d key to finish.
# cd /etc/profile.d
cat > java.sh
export JAVA_HOME=/opt/java
export PATH=$PATH:$JAVA_HOME/bin
^d
I then added the following to my library paths
echo /opt/java/jre/lib >> /etc/ld.so.conf
echo /opt/java/jre/lib/i386 >> /etc/ld.so.conf
echo /opt/java/jre/lib/i386/server >> /etc/ld.so.conf
# /sbin/ldconfig
I added the following to the top of my /etc/rc.d/init.d/httpd
export LD_LIBRARY_PATH=:/opt/java/jre/lib/i386:/opt/java/jre/lib/i386/server
I then exited and logged back in. Made sure I could run java and javac.
Then I recopiled php.
make clean
make distcleam
./configure –with-java=/opt/java (plus all the other includes you need)
make
make install
Important
in the /usr/local/lib/php/
setup a sim link
ln -s java.so libphp_java.so
I added this to my php.ini
[Java]
java.class.path = /usr/local/lib/php/php_java.jar:/usr/local/lib/php/classes
java.home = /opt/java
java.library = /opt/java/jre/lib/i386/libjava.so
java.library.path = /usr/local/lib/php/extensions/no-debug-non-zts-20020429
extension_dir = /usr/local/lib/php/extensions/no-debug-non-zts-20020429
extension = java.so
Restart Apache and test with this script.
//echo phpinfo();
// 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’).’
‘;
print ‘Java vendor=’ .$system->getProperty(’java.vendor’).’
‘;
print ‘OS=’.$system->getProperty(’os.name’).’ ‘.
$system->getProperty(’os.version’).’ on ‘.
$system->getProperty(’os.arch’).’
‘;
// 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’));
Have Fun
Leave a Reply
You must be logged in to post a comment.
