PHP & Java

Source: Internet
Author: User
MarkNoldJoostSoeterbroekTheJavaextensionisanextremelyexcitingtool. Bylearninghowtousethismodule, youcanextendPHPbythepowerofallavailableJavaclasses. Toshowyoutheba Mark Nold
Joost Soeterbroek

 

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 varous MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.

Step 1: Install the JDK. This is fairly simple, as the JDK installwithout implements 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 IgnoresThe 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.dlljava.library.path=c:\web\php4\extensionsjava.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses"

Typically, people putExtension = php_java.dllDirective with the rest of the extensions, but it can sit happily under [java].Java. library. pathMust be set in the location of the php_java.dll, andJava. class. pathMust include the locationPhp_java.jar. The java. class. path shocould 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 shoshould 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:

 


  getProperty("java.version")." 
\n";print "Java vendor=".$system->getProperty("java.vendor")."

\n\n";print "OS=".$system->getProperty("os.name")." ".$system->getProperty("os.version")." on ".$system->getProperty("os.arch")."
\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 shocould see some results like:

 

Java version=1.2.2Java vendor=Sun Microsystems Inc.OS=Windows 95 4.10 on x86Wednesday, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.