php calls the java method: first download "php-java-bridge_6.2.1_documentation.zip" and run JavaBridge; then create a new test.php in the www directory; finally call the custom JAVA class in PHP. Can.
![How to call java method in php](https://img.php.cn/upload/article/202010/22/2020102209461114357.jpg)
Recommended: "PHP Video Tutorial" "java Video Tutorial"
1. PHP calls JAVA method
1.1. Software requirements
- Download php-java-bridge_6.2.1_documentation.zip, the download address is: http://php-java-bridge.sourceforge .net/pjb/download.php
- JavaBridge.jar: Unzip the above php-java-bridge_6.2.1_documentation.zip to get a JavaBridge.war, rename JavaBridge.war to JavaBridge.jar, again Use WinRAR to decompress JavaBridge.jar to the JavaBridge directory. JavaBridge.jar and Lucene.jar can be found in JavaBridge\WEB-INF\LIB.
- JDK: JavaBridge is implemented by the Java language, so JDK must be installed to provide support for jar file execution. And configure the environment variables.
1.2. Run JavaBridge
- Double-click to run JavaBridge.jar in JavaBridge\WEB-INF\LIB. A dialog box that can be selected should pop up. If it does not pop up , because the JDK is not installed or there is a file association error. The solution is to install the JDK or run "start javaw -jar JavaBridge.jar" (the content is saved in *.bat, and *.bat is in the same directory as JavaBridge.jar) instead of double-clicking . When the dialog box pops up, there is no need to make a selection, just click "OK". Correct as follows:
![](https://img.php.cn/upload/article/000/000/020/29c497a80da52f2d789c110175626519-0.png)
1.3. Test JavaBridge
- Create a new test.php in the www directory with the following content:
require_once("java/Java.inc"); $system = new Java('java.lang.System'); $s = new Java("java.lang.String", "php-java-bridge config...
"); echo $s; print 'Java version='.$system->getProperty('java.version').'
'; print 'Java vendor=' .$system->getProperty('java.vendor').'
'; print 'OS='.$system->getProperty('os.name').' '. $system->getProperty('os.version').' on '. $system->getProperty('os.arch').'
'; $formatter = new Java('java.text.SimpleDateFormat', "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java('java.util.Date')).'
'.'
';
Copy after login
- Find JavaBridge.jar in JavaBridge\WEB-INF\LIB, decompress JavaBridge.jar and you can find the java directory in JavaBridge\META-INF. Completely copy the java directory to the same directory as test.php. Then run: http://localhost/test.php, the following content will be output, indicating that the JavaBridge installation is successful
![](https://img.php.cn/upload/article/000/000/020/29c497a80da52f2d789c110175626519-1.png)
1.4. Call customization in PHP JAVA class
- First create a test class Test.java:
public class Test { private String name = ""; // setter and getter public void setName(String name) { this.name = name; } public String getName() { return this.name; } //加法 public float add(float num1, float num2) { return num1 + num2; } }
Copy after login
- After writing the class, compile Test.java to generate a class file, and convert Test. Class is copied to the C:\Program Files\Java\jre7\classes directory. Because of different version numbers, the name of the jre7 folder may be different. When installing the JDK, there is no C:\Program Files\Java\jre7\classes by default and needs to be created manually.
- Modify the content of Test.php to:
//自定义类测试 require_once("java/Java.inc"); $test = new Java("Test"); //产生实例 $test->setName("哈哈,PHP调用JAVA的方法!"); //后面的调用就跟在php中调用类方法一样 print "调用类Test的getName方法,返回值为:".$test->getName()."
"; print "调用Test的add方法,返回值为:".$test->add(11.2, 15.7);
Copy after login
- Then visit http://localhost/test.php, the page will output the following content:
![](https://img.php.cn/upload/article/000/000/020/29c497a80da52f2d789c110175626519-2.png)
- It took me a day to work on this thing. I found many methods on the Internet but it didn’t work. It is inconvenient to move the class to the jre directory every time. Add java_require ("Test.jar"); This error is reported when quoting the jar package: Warning: java_require() not supported anymore. Please use
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