Home > Article > Backend Development > Detailed explanation of how php calls its own java program, detailed explanation of how php calls java_PHP tutorial
The example in this article describes the implementation method of PHP calling its own Java program. Share it with everyone for your reference, the details are as follows:
It goes without saying that I need to install jdk at the beginning. I installed java ee 5 jdk
1. Unzip the downloaded php-java-bridge_5.2.2_j2ee.zip. There is a JavaBridge.war and open it directly with winrar. Go to WEB-INF/lib/JavaBridge.jar and copy this jar package to the ext/ directory of your php directory.
2. Open the war package, there is a java folder inside, copy it all to your PHP project, such as /demo/java
3. The current version is VMBridge. If you want PHP to call the java class, you must first start JavaBridge,
Call java –jar JavaBridge.jar from the command line or double-click JavaBridge.jar and select listening port 8080 in the pop-up window
For easy startup in the future, I created a new bat file under ext/ with the following content:
@echo off start javaw -jar JavaBridge.jar
After saving, double-click to start. There will be a prompt box to select vmbridge port. The default is 8080. Just click ok
4. Create a new test.php under /demo/ with the following content:
<?php require_once("java/Java.inc"); header("content-type:text/html; charset=utf-8″); // get instance of Java class java.lang.System in PHP $system = new Java('java.lang.System'); $s = new Java("java.lang.String", "php-java-bridge config…<br><br>"); echo $s; // demonstrate property access print 'Java version='.$system->getProperty('java.version').' <br>'; print 'Java vendor=' .$system->getProperty('java.vendor').' <br>'; print 'OS='.$system->getProperty('os.name').' '. $system->getProperty('os.version').' on '. $system->getProperty('os.arch').' <br>'; // java.util.Date example $formatter = new Java('java.text.SimpleDateFormat', "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java('java.util.Date')); ?>
5. Start apache and view http://localhost/demo/test.php in the browser
You will see the following information:
Copy code The code is as follows: php-java-bridge config…
Java version=1.6.0_10
Java vendor=Sun Microsystems Inc.
OS=Windows Vista 6.0 on x86
Sunday, November 23, 2008 at 4:31:49 PM China Standard Time
Custom JAR:
package ttt; 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)); } } } }
Generate it as JAR and copy it to the D drive.
/demo/index2.php
<? require_once("java/Java.inc"); java_require("D://1.jar"); $myj = new Java("ttt.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"; ?>
View http://localhost/demo/index2.php in your browser
Method 2: php_java.dll needs to configure php.ini, the new version of php-java-bridge does not have dll files
First make sure that your PHP and Apache servers and JDK (or JRE are also acceptable) have been installed
Download php-java-bridge online (find it yourself or http://sourceforge.net/project/showfiles.php?group_id=117793)
Decompress the downloaded php-java-bridge. After decompression, there will be a JavaBridge.war in the folder. Then decompress this JavaBridge.war in the same way (win rar can decompress it)
After decompression, you can find java-x86-windows.dll from the cgi folder in the WEB-INF folder, and JavaBridge.jar
Copy java-x86-windows.dll and JavaBridge.jar to the PHP plug-in folder (in my case it is C:/AppServphp/ext), and change java-x86-windows.dll to php_java.dll
Modify php.ini file
If php.ini does not originally have the following content, please add it yourself. If it originally has the following content, please modify it to the following [I am using JDK]
extension=php_java.dll
[Java] ;java.java = "C:\jdk1.6.0_13\bin\java" java.class.path = "D:\php\ext\JavaBridge.jar;c:\myclasses" c:\myclasses可自定义,用来存放自己写的JAVA文件 java.java_home = "C:\jdk1.6.0_13\jre" java.library = "d:\jdk1.2.2\jre\bin\server\jvm.dll" java.library.path = "D:\php\ext"
Restart Apache and check phpinfo
java java support Enabled java bridge 3.0.8 java.java_home C:\jdk1.6.0_13 java.java C:\jdk1.6.0_13\bin\java java.log_file <stderr> java.log_level no value (use backend's default level) java.ext_java_compatibility Off java command C:\jdk1.6.0_13\bin\java -Djava.library.path=D:\php\ext -Djava.class.path=D:\php\ext/JavaBridge.jar -Djava.awt.headless=true php.java.bridge.JavaBridge INET_LOCAL:0 2 java status running java server 9267
Check whether the second to last item, java status, is not running (this is because you have not started JavaBridge.jar). If it changes to running cfa3a9ff591e1912334b6e163f332100foo = (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程序设计有所帮助。