How do I install the JDK?
All the way to the JDK installation (assuming that the C:\JDK directory is installed and PHP is installed in C:\PHP4). Then do the following steps:
Added under Win9x: "path=%path%; C:\jdk\bin "to Autoexec.bat file, add under NT"; C:\jdk\bin "into the environment variable. This step is very important, so that PHP can correctly find the Java class that needs to be called.
How do I modify the php.ini file to support Java?
[Java] Extension=php_java.dll Java.home = C:\jdk1.3.1_01 Java.library = C:\jdk1.3.1_01\jre\bin\CLASSIC\jvm.dll Java.library.path=c:\php4\extensions\ Java.class.path= "C:\php4\java\php_java.jar; C:\php4\java\ " |
Add Extension=php_java.dll to the php.ini and note that it should be added to the [Java] segment instead of being combined with other extension=*.dll. Set the Java.class.path, let it point to Php_java.jar, if you use the new Java class, you should also deposit this path, in this example, we use C:\php4\java\ this directory.
Test environment
Create the following PHP file:
$system = new Java ("Java.lang.System"); Print "Java version=". $system->getproperty ("Java.version"). " n "; Print "Java vendor=". $system->getproperty ("Java.vendor"). "<p>\n\n"; <br= "" >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 "; ? > |
If you install it correctly, you will see information similar to the following:
Java version=1.3.1_01 Java Vendor=sun Microsystems Inc.
Os=windows 98 4.10 on x86 Monday, November, 2001 at 9:45:53 am Greenwich Mean time |
In this way, we have successfully built a PHP runtime environment that can be used with Java classes.
Application examples
Create your own Java class, creating a new Phptest.java file and placing it in your Java.class.path directory (in the example above, "C:\php4\java\"), the file content is as follows:
public class phptest{ Public String myvalue; public string SetValue (String v) { Myvalue=v; return myvalue; } public static void Main (String args[]) { Phptest p = new Phptest (); if (Args.length = = 0) { System.out.println ("not set!"); }else{ for (int i=0; i < args.length; i++) { String arg = args[i]; System.out.println (P.setvalue (ARG)); } } } } |
After creating this file, we will compile this file and use the Javac Phptest.java command on the DOS command line.
To test this Java class with PHP, we create a phptest.php file with the following content:
$myobj = new Java ("Phptest"); echo $myobj->setvalue ("hello,php and Java world!"); ? > |
If you get this warning message: java.lang.ClassNotFoundException error, this means your Phptest.class file is not in your Java.class.path directory.
Note that Java is a mandatory type language, and PHP is not, so that when we merge them, it is easy to lead to errors, so when we pass the variable to Java, we should correctly specify the type of the variable.
(go) Use Java's class library in PHP.