欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

在PHP中调用JAVA_PHP

程序员文章站 2022-05-02 11:50:17
...
安装PHP4.3.5RC3.dev(Win32) Apache 2.0.52(For Win32)和j2sdk 1.4 (For Win32)都不用说了,如果不会安装,请看相关手册。
在PHP配置文件php.ini中找到 " ;extension=php_java.dll " 这上行,并把前面的分号去掉;
再找到:
[Java]
;java.class.path =
;java.home =
;java.library =
;java.library.path =

改为:
java.class.path = "JAVA的CLASS_PATH;(将要放置自己的class文件的目录)d:\java_for_php"
java.home = "JAVA的JAVAHOME"
java.library = "JAVA的JAVAHOME\jre\bin\server\jvm.dll"
java.library.path = "PHP安装目录\extensions\"

修改好后,php.ini 文件必须放在Windows安装目录内,重新启动Apache。

在d:\java_for_php内新建一个PhpCallJava.java ,内容如下:
public class PhpCallJava {
public String testString;

public String test (String str) {
if (str.equals("")) {
return "This is a empty string!";
} else {
return str;
}
}

public String GetTestString () {
return "Your string is: " testString;
}
}
编译PhpCallJava.java 生成PhpCallJava.class

新建一个PHP文件,内容如下:
php

$myjava = new Java("PhpCallJava");
echo "Test Result are ".$myjava->test("Hello JAVA, I m PHP.")."
\n
";

$myjava->testString = "JAVA, I gave you a string.";
echo "You have set string is: ".$myjava->testString."
\n
";
echo "My java method reports:".$myjava->GetTestString ()."
\n
";
?>

结果:
Test Result are Hello JAVA, I m PHP.
You have set string is: JAVA, I gave you a string.
My java method reports:Your string is: JAVA, I gave you a string.