php当然可以调用JAVA类库
1。安装完JDK后
在 Win9x 下加入 :“PATH=%PATH%;C:\jdk1.2.2\bin” 到AUTOEXEC.BAT文件中
在 NT 下加入 “;C:\jdk1.2.2\bin”到环境变量中。
linux的我不太清楚
2。修改你的PHP.INI文件。
[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
并在[java]中,设定好java.class.path,让它指向php_java.jar,如果你使用新的JAVA类,你也应该存入这个路径,假设使用c:\myclasses这个目录。
3。假设已经新建一个phptest.java文件,
public class phptest{
/**
* A sample of a class that can work with PHP
* NB: The whole class must be public to work,
* and of course the methods you wish to call
* directly.
*
* Also note that from PHP the main method
* will not 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 is 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(p.test(arg));
}else{
for (int i=0; i < args.length; i++) {
String arg = args[i];
System.out.println(p.test(arg));
}
}
}
} 将它放置在你的java.class.path目录下,php的调用语句:
<?php $myj = new Java("phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>"; $myj->foo = "A String Value";
echo "You have set foo to <b>" . $myj->foo . "</b><br>n";
echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>n"; ?> 如果你得到这样的警告信息:java.lang.ClassNotFoundException error ,这就意味着你的phptest.class文件不在你的java.class.path目录下。
1。安装完JDK后
在 Win9x 下加入 :“PATH=%PATH%;C:\jdk1.2.2\bin” 到AUTOEXEC.BAT文件中
在 NT 下加入 “;C:\jdk1.2.2\bin”到环境变量中。
linux的我不太清楚
2。修改你的PHP.INI文件。
[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
并在[java]中,设定好java.class.path,让它指向php_java.jar,如果你使用新的JAVA类,你也应该存入这个路径,假设使用c:\myclasses这个目录。
3。假设已经新建一个phptest.java文件,
public class phptest{
/**
* A sample of a class that can work with PHP
* NB: The whole class must be public to work,
* and of course the methods you wish to call
* directly.
*
* Also note that from PHP the main method
* will not 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 is 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(p.test(arg));
}else{
for (int i=0; i < args.length; i++) {
String arg = args[i];
System.out.println(p.test(arg));
}
}
}
} 将它放置在你的java.class.path目录下,php的调用语句:
<?php $myj = new Java("phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>"; $myj->foo = "A String Value";
echo "You have set foo to <b>" . $myj->foo . "</b><br>n";
echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>n"; ?> 如果你得到这样的警告信息:java.lang.ClassNotFoundException error ,这就意味着你的phptest.class文件不在你的java.class.path目录下。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货