It has been a long time since I did not develop PHP. recently, to modify an original project, I must support calling Java classes and start to re-operate PHP. I will solve the configuration problem first. It has been a long time since I did not develop PHP. recently, to modify an original project, I must support calling Java classes and start to re-operate PHP. I will solve the configuration problem first. Don't talk too much. There are a lot of articles on the Internet, but there are a lot of errors in it. next I will introduce the entire configuration process and share it with you. if you have any questions, please contact us if you are willing to solve them.
Note: the normality of this article is only guaranteed for the PHP and JDK versions provided in this article. Other versions are not tested one by one. if you have any questions, please contact us to solve the problem with you.
The installation and configuration of PHP is skipped here
Test Environment: Windows2003 + IIS + PHP Version 4.3.6 + JDK 1.4.1 _ 01
Below I will first list the incorrect configurations on the Internet as follows:
Error 1
Java. home = D: \ j2sdk14101 \
The error result is as follows:
Can't open D: \ j2sdk14101 \ lib \ tzmappings.
Solution:
Java. home = D: \ j2sdk14101 \ jre
Error 2
Java. class. path = "C: \ PHP \ extensions \ php_java.jar; x: \ Java Class file storage directory \
The error result is as follows:
Fatal error: Call to a member function on a non-object in E: \ inberkong \ public_html \ phproot \ testjava. php on line
Error cause: the user's Java class file is not stored in the x: \ Java Class file storage directory \ or the x: \ Java Class file storage directory \ does not exist
Solution: 1. confirm java. class. path = "C: \ PHP \ extensions \ php_java.jar; x: \ Java Class file directory \ correct
2. check whether the Class file called in PHP is in this directory and make sure it is correct.
The correct configuration is as follows:
Step 1: Make sure the environment is correct
The preceding environment is correctly configured by default.
PHP Directory: C: \ PHP
JDK Directory: D: \ j2sdk14101
Step 2
Find php. ini in C: \ WINDOWS
1. locate extension_dir and configure php_java.jar php_java.dll in the directory.
Extension_dir = "D: \ PHP \ extensions"
2. extension = php_java.dll: remove the semicolon before the configuration information.
Extension = php_java.dll
3. find the configuration items after [java ].
[Java]
Java. class. path = "C: \ PHP \ extensions \ php_java.jar; C: \ PHP \ php_for_class \"
Java. home = D: \ j2sdk14101 \ jre
Java. library = D: \ j2sdk14101 \ jre \ bin \ server \ jvm. dll
Java. library. path = C: \ PHP \ extensions \
Step 3
Write a Java file and compile it in C: \ PHP \ php_for_class \
The Test. java file is as follows:
Package Test;
Public class Test
{
Public String getMessage (String dd)
{
Return "Hello PHP! I am from JAVA. "+ dd;
}
}
Write a php file testjava. php and call the Test class in the Test package.
$ System = new Java ("java. lang. System ");
Print "Java version =". $ system-> getProperty ("java. version ")."
\ N ";
Print "Java vendor =". $ system-> getProperty ("java. vendor ")."
\ 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 ";
$ B = new Java ("Test. Test ");
$ X = $ B-> getMessage ("
Hello Java! I am from PHP! ");
Echo $ x;
?>
Display result:
Java version = 1.4.1 _ 01
Java vendor = Sun Microsystems Inc.
OS = Windows XP 5.2 on x86
Thursday, March 13, 2006 at 3:00:51 pm China Standard Time
Hello PHP! I am from JAVA.
Hello Java! I am from PHP!
The above is the content of common configuration errors for PHP to call java. For more articles, please follow the PHP Chinese network (www.php1.cn )!