Java is a very powerful programming tool, its extension library is also very useful, this tutorial, mainly on how to use PHP invoke powerful Java class library (classes). To facilitate your study, this tutorial will include Java installation and some basic examples.
Installation under Windows
Step One: Install the JDK, it's very easy, you just have to install it all the way to the carriage. Then do the following steps.
Add under Win9x: "path=%path%; C:\jdk1.2.2\bin "to the Autoexec.bat file
Add under NT "; C:\jdk1.2.2\bin "to the environment variable.
This step is very important so that PHP can correctly find the Java class that needs to be invoked.
Step Two: Modify 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"
在PHP.INI中加入extension=php_java.dll
And in [Java], set the Java.class.path to point to Php_java.jar, and if you use the new Java class, you should also deposit this path, and in this example we use the C:\myclasses directory.
Step three: Test the environment, create the following PHP file:
<?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";
?>
If you install it correctly, you will see the following information:
Java version=1.2.2
Java Vendor=sun Microsystems Inc.
Os=windows 4.10 on x86
Wednesday, October at 10:22:45 AM-Standard time
In this way, we have successfully built a PHP runtime environment that can use Java classes, and we can start the course we're going to take.
Example 1: Creating and using your own Java class
It is easy to create your own Java class. Create a new Phptest.java file and place it in your Java.class.path directory, which reads as follows:
public class phptest{
/**
* A sample of a class that can work with PHP
* Nb:the whole class must to work,
* And of course the methods you wish to call
* directly.
*
* Also Note this from PHP's Main method
* Won't be called
*/
Public String foo;
/**
* Takes a string and returns the result
* or a msg saying your string was empty
*/
public string Test (string str) {
if (Str.equals ("")) {
str = "Your string was empty.";
}
return str;
}
/**
* Whatisfoo () simply returns the value of the variable foo.
*/
Public String Whatisfoo () {
Return ' foo is ' + foo;
}
/**
* This is called if phptest be run from the command line with
* Something like
* Java phptest
* OR
* Java phptest Hello there
*/
public static void Main (String args[]) {
Phptest p = new Phptest ();
if (Args.length = = 0) {
String arg = "";
System.out.println (ARG) (p.test);
}else{
for (int i=0 i < args.length; i++) {
String arg = args[i];
System.out.println (ARG) (p.test);
}
}
}
}