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";
?>
如果你正确安装了,你将会看到以下信息:
复制代码 代码如下:Java version=1.2.2
Java vendor=Sun Microsystems Inc.
OS=Windows 95 4.10 on x86
Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time
理解如何调用JAVA很重要,下一步我们就要创建自己的JAVA文件,让PHP来调用,JAVA文件的java.class.path很重要
创建和使用你自己的JAVA类 [注意大小写]
创建你自己的 JAVA 类非常容易。新建一个 phptest.java 文件,将它放置在你的 java.class.path 目录下【 c:\myclasses】,文件内容如下:
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));
}
}
}
}
创建这个文件后,我们要编译好这个文件,在 DOS 命令行使用 javac phptest.java 这个命令。
为了使用 PHP 测试这个 JAVA 类,我们在web目录下创建一个 phptest.php 文件,内容如下:
" . $myj->test("Hello World") . "";
$myj->foo = "A String Value";
echo "You have set foo to " . $myj->foo . "
\n";
echo "My java method reports: " . $myj->whatisfoo() . "
\n";
?>
如果你得到这样的警告信息:java.lang.ClassNotFoundException error ,这就意味着你的 phptest.class 文件不在你的 java.class.path 目录下。
注意的是 JAVA 是一种强制类型语言,而 PHP 不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678″;
这只是一个很小的例子,你可以创建你自己的 JAVA 类,并使用 PHP 很好的调用它!关键在于理解java.class.path目录的重要性。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。