Home > Backend Development > PHP Tutorial > Common configuration errors when PHP calls java classes

Common configuration errors when PHP calls java classes

黄舟
Release: 2023-03-03 14:14:01
Original
1466 people have browsed it

I haven’t been engaged in PHP development work for a long time. Recently, I modified an original project to support calling Java classes. I started to work on PHP again and solved the configuration problem first. Don't talk too much. There are many articles on the Internet, but there are many wrong things in them. Let me introduce the whole configuration process to share with you. If you have any questions, please feel free to contact me and I will help you solve it.

Note: The normality of this article is only guaranteed for the versions of PHP and JDK provided in this article. Other versions have not been tried one by one. If there are any problems, please contact us in time and we are willing to work with you to solve the relevant problems.

The work of installing and configuring PHP will be skipped here
Test environment: Windows2003+IIS+PHP Version 4.3.6+JDK 1.4.1_01

Now I will list the incorrect configuration list on the Internet as follows:

Incorrect configuration 1
java.home = D:j2sdk14101
The error result is as follows:
can't open D:j2sdk14101\libtzmappings.
Solution:
java.home = D:j2sdk14101jre

Error configuration 2
java.class.path="C:PHPextensionsphp_java.jar ;x:Java Class file is stored in the directory
The error result is as follows:
Fatal error: Call to a member function on a non-object in E:inberkongpublic_htmlphproottestjava.php on line
Cause of error: The user's Java class file is not stored in x:Java Class The file is stored in the directory or the directory where the x:Java Class file is stored does not exist
Solution: 1 Make sure java.class.path = "C:PHPextensionsphp_java.jar; Call the Class file and make sure it is correct

The correct configuration is as follows:

Step 1: Make sure the environment is correct

The default environment configuration above is correct
PHP directory: C:PHP
JDK directory: D:j2sdk14101
Second Step
Find php.ini under C:WINDOWS
1 Find extension_dir and configure to ensure that there is php_java.jar php_java.dll in its directory
extension_dir = "D:PHPextensions"
2 Find;extension=php_java.dll before this configuration information Remove the semicolon
extension=php_java.dll
3. Find the content after [java] in the file and configure the related content
[Java]
java.class.path = "C:PHPextensionsphp_java.jar;C:PHPphp_for_class"
java.home = D:j2sdk14101jre
java.library = D:j2sdk14101jrebinserverjvm.dll
java.library.path = C:PHPextensions
Step 3

Write a Java file and compile it and put it in C:PHPphp_for_class

Test.java The 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 to 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")."

nn";
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;

?>

show Result:
Java version=1.4.1_01
Java vendor=Sun Microsystems Inc.
OS=Windows XP 5.2 on x86
Thursday, January 12, 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 when PHP calls java classes. For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template