Home  >  Article  >  Backend Development  >  How to call java method in php

How to call java method in php

藏色散人
藏色散人Original
2020-10-22 09:50:014715browse

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

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:

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...<br><br>");

echo $s;

print &#39;Java version=&#39;.$system->getProperty(&#39;java.version&#39;).&#39; <br>&#39;;

print &#39;Java vendor=&#39; .$system->getProperty(&#39;java.vendor&#39;).&#39; <br>&#39;;

print &#39;OS=&#39;.$system->getProperty(&#39;os.name&#39;).&#39; &#39;.

$system->getProperty(&#39;os.version&#39;).&#39; on &#39;.

$system->getProperty(&#39;os.arch&#39;).&#39; <br>&#39;;

$formatter = new Java(&#39;java.text.SimpleDateFormat&#39;,

"EEEE, MMMM dd, yyyy &#39;at&#39; h:mm:ss a zzzz");

print $formatter->format(new Java(&#39;java.util.Date&#39;)).&#39; <br>&#39;.&#39; <br>&#39;;
  • 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

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;

    }

}
  • 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()."<br>";

print "调用Test的add方法,返回值为:".$test->add(11.2, 15.7);
  • Then visit http://localhost/test.php, the page will output the following content:
  • 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 fa9cd317b0b61a93533c6caa2349d3eetomcat or jee hot deployment5db79b134e9f6b82c0b36e0489ee08ed instead in D:\wamp\www\java\Java.inc on line 1656 I don’t know how to directly reference the jar package. If you know how to do it, please give me some advice. !

The above is the detailed content of How to call java method in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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