Linux Systems Administrator
Jupitermedia
US-AZ-Tucson

Justtechjobs.com Post A Job | Post A Resume

PHP & Java

The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.
Windows Installation
The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.x\bin directory is in your path.
This will make compiling your Java Classes easier. On Win9x add
PATH=%PATH%;C:\jdk1.2.2\bin
to your autoexec.bat. On NT add
;C:\jdk1.2.2\bin
to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.
Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file.
[java]
extension=php_java.dll
java.library.path=c:\web\php4\extensions\
java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses"
Typically, people put the extension=php_java.dll directive with the rest of the extensions, but it can sit happily under [java]. The java.library.path must be set in the location of the php_java.dll, and java.class.path must include the location of php_java.jar. The java.class.path should also include the path to other classes you may wish to use (note the double quotes!). In these examples, we will be talking about c:\myclasses. You should also note that the single period is ignored by PHP and Java. As far as we know, you cannot set PHP to look in the current directory for its Java classes.
Step 3: Testing your Install. Now, you're ready to go. Create a PHP file that looks something like this:
<?php

  $system = new Java("java.lang.System");
  print "Java version=".$system->getProperty("java.version")." <br>\n";
  print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n";
  print "OS=".$system->getProperty("os.name")." ".
              $system->getProperty("os.version")." on ".
              $system->getProperty("os.arch")." <br>\n";

  $formatter = new Java("java.text.SimpleDateFormat","EEEE, 
	MMMM dd, yyyy 'at' h:mm:ss a zzzz");
  print $formatter->format(new Java("java.util.Date"))."\n";
  
?>
NB: This is taken directly from Sam Ruby's examples. If you have correctly installed everything, you should see some results like:
Java version=1.2.2 
Java vendor=Sun Microsystems Inc. 
OS=Windows 95 4.10 on x86 
Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time 
A very simple example, but it shows you can access currently available Java classes. Once you have this example working, you have successfully set up the PHP Java extension.
[ Next Page ]


Comments:
PHP & Java on LINUX - Page cannot be displayeapril07/25/07 04:56
RE: php & java errordishan04/18/07 03:38
Please help... Header ErrorJavier11/11/05 14:30
How to return PHP script from Servlets?Guru08/19/05 00:27
php and linuxmadhur06/28/05 02:25
php.ini problem?gelo05/26/05 23:45
need urgent help on this...tnx a lot!!!gelo05/26/05 23:43
RE: PHP to Java ( Calling Java Tag Lib)john05/09/05 02:35
fetching some value into java appletraj03/29/05 02:49
RE: cannot instantiate non existent class errorJagruti03/10/05 08:24
PHP on web logicKaran03/10/05 03:35
php - javaAlnoor Anwar02/06/05 11:36
RE: Calling PHP within JAVAPoetBeast01/18/05 15:21
Calling Java from PHPphuc12/12/04 01:16
Calling Java from PHPFabiola Sanchez10/14/04 18:27
RE: javax.commBernd Peltzer-Bartels08/25/04 05:22
RE: PHP + Java Warning:Unknown listdon-carlos06/22/04 22:18
RE: Java & PHP on UNIX Platforms !!girionis11/23/03 15:36
RE: PHP & JAVA APPLET?Ch. Anil Kumar05/15/03 01:44
Please see in the part of extensions_dirmr.PonD02/09/03 04:28
RE: Java & PHP on UNIX Platforms !!Dave12/13/02 23:18
IE 6.0 and netscape 7.00 errorReza11/05/02 23:20
RE: PHP Java Linux Apache WORKS!lmx10/30/02 22:29
php-java for redhat linux 7.2Versace10/22/02 11:10
RE: php & java on SolarisGopu N10/16/02 15:06
php & java errorjang10/14/02 08:14
how to make php call a servlet?Pavani10/14/02 00:47
PHP & Java:ProblemsRussel09/29/02 06:24
Unable to create Java Virtual Machine fixed!Nico09/17/02 06:33
PHP & EJB??gaucho09/09/02 07:06
RE: php and tomcatvijay khanna08/25/02 01:47
php and tomcatNO-MIND08/19/02 15:46
RE: php & java on Solariswoo-hwan,park08/18/02 09:24
RE: php & java on SolarisWoo-hwan, Park08/18/02 01:56
PHP Java and AWTThomas08/13/02 09:45
RE: Calling PHP within JAVADiego Dal Cero08/12/02 08:18
RE: Solution for NoClassDefFounder:Error Khalil M.08/12/02 03:16
RE: Solution for NoClassDefFounder:Error Madalin Bucur08/06/02 15:26
Solution for NoClassDefFounder:Error Khalil M.07/30/02 09:18
RE: how to use php and crontabluke woollard07/29/02 19:36
NoClassDefinitionFoundNeoXL07/25/02 09:03
RE: php & java on SolarisN Gopu07/24/02 18:37
NoClassDefFoundError: net/php/reflectHrant07/18/02 05:24
php & java on SolarisGopu N07/01/02 23:56
RE: php & javadharm shankar06/22/02 21:50
RE: PHP + Java Warning:Unknown listJaromir Dzialo06/20/02 10:14
RE: Attempt to access a Java propertyJaromir Dzialo06/20/02 10:12
how to use php and crontabRich06/16/02 22:22
RE: PHP + Java Warning:Unknown listEtienne06/14/02 07:58
FATAL ERROR: Unable to start Java Virtual...jason06/12/02 19:06
Java/PHP Returns no data errorJason Korkin06/12/02 08:25
RE: Php and Java Please help me in confraintung06/10/02 05:27
Php and Java Please help me in confKalpan Vyas06/07/02 02:19
Attempt to access a Java propertyredpool05/29/02 22:27
Real advantage?Vegeta05/19/02 04:21
php, java y mysqlJose05/13/02 23:50
PHP + Java Warning:Unknown listWes05/02/02 14:00
PHP & Java ProblemAnfaz04/30/02 05:36
RE: Crontabvasanth04/24/02 07:24
javax.commcedric04/23/02 10:48
RE: php with java on linuxdave04/17/02 02:40
jscript in phpJoseph Bestler04/16/02 15:25
PHP classes and Java servletsPedro Novoa04/16/02 13:38
Is there a way to use Perl classes in PHP?Frederick Clark04/12/02 14:58
php with java on linuxeshwar04/03/02 23:55
Pb when loading JVM => PHP4TS.DLL ?zolive03/26/02 08:05
Opening a new window JavaStylenoob03/19/02 19:42
OnClick, MouseOut, MouseOver in PHP?Do Minh Duc03/10/02 23:21
RE: PHP Java Linux Apache WORKS!kakeung02/27/02 00:26
RE: PHP java extension must match JDK version?bangk02/26/02 23:39
How to get variable's value in php from java Manish02/07/02 05:58
How to Fix the ClassNotFoundException ErrorRichard Wall02/05/02 17:27
mod_php4 with java /ext support freebsd 4.5thomas02/05/02 02:19
Cannot instantiate non-existent class: java Madhusudan01/31/02 05:14
own classes? ...aren't found HELP!!!!Scy01/30/02 10:28
NoClassDefFoundError: net/php/reflectUli01/25/02 05:12
Newbie php and java questionT Toyohara01/24/02 10:44
PHP Java Linux Apache WORKS!Jaison Freed01/18/02 17:16
Calling PHP within JAVABrey01/16/02 11:39
Java & PHP on UNIX Platforms !!Chris Arnett12/28/01 20:59
cannot instantiate non existent class errorK naga subramanian12/27/01 23:00
How to create thread using php?venkateshwaran12/27/01 01:56
any help available:php -java on linuxK naga subramanian12/26/01 01:50
php java loader.Jeanlo12/20/01 12:44
dynamic includecalamar12/18/01 02:51
InteroprerabilityAnand12/17/01 02:47
Running PHP as a servlet under Tomcatneil912/09/01 21:39
about phpt.sivakumar12/03/01 11:43
Does all this stuff work for jdk 1.1.8 tooBryan11/19/01 13:31
RE: Calling JAVA Applications with PHP on W2KHans-Jörg Alles11/19/01 08:51
new jvm for each php requestalan sparago11/14/01 17:05
If you don't want to modify php.ini...Lux11/06/01 20:10
RE: "Unable to load Java Virtual Machine" fixed!kim10/29/01 00:02
read this if this is your first php extensionhans anderson10/23/01 17:48
PHP java extension must match JDK version?hans anderson10/19/01 14:39
xalan 2 with phpMax Zilla10/14/01 07:06
own classes? ...aren't found :(((( HELP!!!!Boris10/12/01 12:36
own classes???? Doesn't work :(((Boris10/12/01 12:34
RE: benefits using ext/java Yoway Buorn09/19/01 16:50
RE: unable to start JVM - IMPORTANT NOTE!!!!Darren09/06/01 19:43
RE: Unable to create JVM after reloadGustavo Vázquez08/14/01 12:12
Does PHP cache Java?Monchai W.08/13/01 11:15
benefits using ext/java vanesa08/12/01 12:28
Calling JAVA Applications with PHP on W2KJohn Jason Brzozowski08/02/01 16:10
php & jdbcvanesa07/31/01 01:53
RE: Unable to create JVM after reloadWilson07/25/01 02:31
RE: Unable to create JVM after reloadJohn07/25/01 02:16
Fix 2: Unable to create Java Virtual MachineArtiom Neganov07/24/01 07:16
unable to start JVM maybe fixed ??Pascal Molli07/24/01 01:34
USAR JDBC DESDE PHPvanesa07/20/01 12:16
RE: JDK 1.3 anyoneGerardo Velez07/18/01 14:52
RE: where can I get a copy of php_java.dllRadu Mateescu07/12/01 19:59
Cannot instantiate non-existent class: java davide07/05/01 05:22
RE: php learndoc07/03/01 22:21
RE: unable to load libjava.soGerardo Velez06/27/01 09:15
RE: "Unable to load Java Virtual Machine" fixed!MadGhigno06/26/01 14:29
php learnkaili06/23/01 18:57
RE: linux againJoe Black06/18/01 15:27
CrontabAnusha & Savitha06/18/01 03:32
RE: php as servlet using tomcatMatthias06/17/01 22:28
php as servlet using tomcatMatthias06/14/01 07:51
PHP & JAVA APPLET?German06/14/01 04:06
serveletupLoad06/08/01 02:42
RE: Unable to create JVM after reloadRoger Boone06/07/01 13:49
PHP & Java on LINUXRicardo Matos06/07/01 12:50
PHP & Java on Linux Ricardo Matos06/07/01 12:33
where can I get a copy of php_java.dllJary Nemo06/05/01 22:29
xalan 2.1.0 informationsnono06/01/01 04:10
Unable to create Java Virtual Machine... Rajarshi Ray05/31/01 22:56
unable to load libjava.soRajarshi Ray05/31/01 22:29
php configure error --with-java on SUSEFilip Sergeys05/29/01 15:11
Does it also work for JAva Applets?German G.05/28/01 00:12
PHP & Java under LinuxCurtis Lacy05/22/01 08:59
Problem with "hidden fields"RnB05/22/01 00:08
PHP + IBM JDK 1.3Knight Chang05/15/01 07:38
passing a string to xalan?Michael Dickson05/13/01 19:49
PHP as servlet.Martin05/11/01 09:08
How to run with application jar file???Mau Nguyen05/10/01 07:14
I NEED HELP WITH ARRAYS!Charlene05/09/01 17:36
RE: PHP<->Java<->MySAP.comFilip Sergeys05/08/01 16:08
Handle exceptions from JavaJailton Oliveira05/07/01 17:43
java support and oracle supportNicole05/07/01 17:28
RE: where are the libraries ?Michael05/05/01 10:10
linux againNicole05/04/01 23:33
where are the libraries ?nono05/02/01 01:17
jdk_sem_post ???MCF04/30/01 15:51
What advantage over just using java?Matt Smith04/30/01 13:03
Can the same be done with classes made in C++Nomad04/25/01 07:51
PHP<->Java<->MySAP.comeXXos04/25/01 06:24
RE: non-existent class error under win98smoglee04/18/01 03:33
Final SolutionZeuZ04/13/01 05:36
RE: Can I find a free tutorial on PHPMichael Svazas04/09/01 01:35
PHP3 and JavaMoh. A04/09/01 00:35
Is Java Fast enough for Databases?John Cameron04/05/01 09:51
Unable to create JVM after reloadMarkus Wolf03/29/01 07:04
RE: Java crashing PHP; EAPI?Chris G.03/28/01 07:25
RE: "php_java.dll not found"-error messagehaogh_china03/28/01 03:34
"Unable to load Java Virtual Machine" fixed!Robin Mthawanji03/21/01 05:05
YAHOO!!! Java Virtual Machine problem solved!Gan Cheng Dee03/19/01 03:53
RE: Java Sometimes WorksGan Cheng Dee03/19/01 03:45
RE: Unable to load php_java.dllGan Cheng Dee03/17/01 10:47
Unable to create Java Virtual Machine alsoGan Cheng Dee03/17/01 03:44
Can I find a free tutorial on PHPJoseph Raj03/15/01 11:39
StringWriterHans03/12/01 03:34
Php 2 Java InconsitencyViet Anh Nguyen03/05/01 22:19
JDK 1.3 running just fine on Win2kJohn03/04/01 12:51
Java Sometimes WorksJohn Gerstenslager03/02/01 08:25
JDK 1.3 anyoneleo02/27/01 10:57
RE: Unable to create Java Virtual Machine...LLE02/25/01 22:26
session_register and Java ClassesCarlos Figueiredo02/23/01 10:17
Unable to load Java Library libjava.soLLE02/21/01 22:44
Unable to create Java Virtual Machine...James Gunn02/21/01 10:35
RE: non-existent class error under win98Jose02/20/01 09:09
"php_java.dll not found"-error messageErik Olofsson02/20/01 06:43
RE: non-existent class error under win98Tim02/18/01 16:36
non-existent class error under win98Betty Cross02/18/01 11:59
RE: Where is php_java.dll and jar Betty Cross02/18/01 11:04
RE: Massive apache error.log on test exampleSeumas Macmhicean02/14/01 02:59
RE: Massive apache error.log on test exampleAlex Akilov02/13/01 12:20
RE: InconsistencyAlex Akilov02/13/01 09:46
Massive apache error.log on test exampleSeumas Macmhicean02/13/01 06:59
InconsistencyAnthony Chang02/12/01 22:47
RE: Linux RPMAlex Akilov02/12/01 07:17
Linux RPMMichael02/12/01 06:11
RE: form PHP to weblogic EJBThomas Risberg02/04/01 18:40
RE: Linux Alex Akilov02/02/01 10:25
RE: retrieving values from servlet to php pageAlex Akilov02/02/01 10:15
RE: Passing parameters to a constructorAlex Akilov02/02/01 10:00
RE: Linux G Bradley02/01/01 13:30
retrieving values from servlet to php pageSanat02/01/01 00:21
Passing parameters to a constructorMichael Svazas01/31/01 23:37
RE: Unable to load php_java.dllAlex Akilov01/31/01 09:58
Unable to load php_java.dllMuhammad Mansoor Ali01/31/01 02:33
RE: Java & Php problemNarayanan01/31/01 00:01
RE: Java & Php problemAlex Akilov01/30/01 09:47
Java & Php problemNarayanan01/30/01 06:29
Where is php_java.dll and jar Narayanan01/30/01 04:21
RE: next issue?Alex Akilov01/26/01 16:42
RE: Java crashing PHP; EAPI?Alex Akilov01/26/01 16:04
next issue?Mark Lybarger01/26/01 13:39
RE: FreeBSD --with-java compile. fmod error!Alex Akilov01/24/01 17:24
RE: FreeBSD --with-java compile. fmod error!Alfredo Ramos01/24/01 13:30
RE: FreeBSD --with-java compile. fmod error!Alfredo Ramos01/24/01 11:13
RE: FreeBSD --with-java compile. fmod error!Alex Akilov01/23/01 14:28
RE: Java crashing PHP; EAPI?Alfredo Ramos01/23/01 11:56
FreeBSD --with-java compile. fmod error!Alfredo Ramos01/23/01 11:51
your-email-addressAlex Akilov01/23/01 11:43
RE: Java crashing PHP; EAPI?Alex DG01/22/01 09:42
Java crashing PHP; EAPI?Clark01/16/01 16:22
RE: non-existent class err under linuxAlex Akilov01/16/01 16:02
RE: non-existent class err under linuxAlex Akilov01/16/01 14:55
RE: non-existent class err under linuxClark01/16/01 13:58
RE: non-existent class err under linuxAlex Akilov01/16/01 12:25
non-existent class err under linuxClark01/16/01 11:00
RE: Unable to create Java Virtual Machine ...Alex Akilov01/12/01 16:42
RE: Unable to create Java Virtual Machine ....Vlad Pavlenko01/12/01 14:33
RE: Global ConsiderationsAlex Akilov01/12/01 11:40
RE: Unable to create Java Virtual Machine ....Alex Akilov01/12/01 11:31
RE: Bad OO in phptest exampleAlex Akilov01/12/01 11:25
RE: Global ConsiderationsJeff Messner01/08/01 10:43
Global ConsiderationsBrett Dutton01/04/01 00:05
Unable to create Java Virtual Machine ....Weihong Hendricks01/03/01 12:27
What if I host my siteOOzy01/02/01 17:29
What I host my siteOOzy01/02/01 17:28
Bad OO in phptest exampleMichael Svazas01/01/01 19:48
RE: NoClassDefFoundError: net/php/refleAlex Akilov12/29/00 13:11
NoClassDefFoundError: net/php/refleSandip12/28/00 19:34
RE: Doesn't workAlex Akilov12/28/00 15:32
RE: PHP to JavaAlex Akilov12/28/00 15:31
RE: Another Error ..Alex Akilov12/28/00 15:22
RE: speed issuesAlex Akilov12/28/00 15:10
RE: speed issuesTim Perdue, PHPBuilder.com12/28/00 11:23
RE: speed issuesJames Hicks12/28/00 11:09
Doesn't workSerhiy Brytskyy12/28/00 08:23
PHP to Javasaradhi12/28/00 06:18
RE: running php as java servletLudo12/27/00 09:14
RE: Another Error ..Philippe Benaiche12/24/00 19:13
PWS Prob;lemMikeM12/24/00 18:43
RE: linux???Jonathan Cox12/24/00 14:13
RE: linux???Mike12/23/00 15:49
linux???paul12/23/00 13:35
RE: speed issuesJustin Grant12/22/00 13:08
RE: speed issuespetedaly12/22/00 10:19
form PHP to weblogic EJBIl Solito Timido12/22/00 09:50
Another Error ..Holev`12/22/00 04:49
cannot load php_java.dllSuharman12/21/00 22:29
running php as java servletNorman Clarke12/21/00 17:54
RE: ErrorTheo Stauffer12/21/00 17:00
ErrorLouie12/21/00 12:54
Strong typingJoshua Pearson12/21/00 12:34
RE: can not load jvm.dllPhilippe Benaiche12/21/00 11:13
can not load jvm.dllgeorge12/21/00 07:44
speed issuesDonncha O Caoimh12/21/00 05:20
Looks ... great !!!LexZEUS12/21/00 02:00
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.