PHP calls the Java method

Source: Internet
Author: User

1. PHP calls the Java method

1.1. Software Requirements

    • Download Php-java-bridge_6.2.1_documentation.zip, for: http://php-java-bridge.sourceforge.net/pjb/download.php
    • Javabridge.jar: Put the above php-java-bridge_6.2.1_ Documentation.zip decompression to get a Javabridge.war, will Javabridge.war renamed to Javabridge.jar, again with WinRAR to extract Javabridge.jar to Javabridge directory, in Javabrid Ge\web-inf\lib inside can find Javabridge.jar and Lucene.jar.
    • Jdk:javabridge is implemented by the Java language, so you must install the JDK implementation to support jar file execution. And the environment variables are well equipped.

1.2. Running Javabridge

    • Double-clicking on the Javabridge.jar running javabridge\web-inf\lib should pop up a dialog box that can be selected, if it does not pop up because there is no JDK or file association error, the workaround is to install the JDK or run the "Start JAVAW -jar Javabridge.jar "(content saved to *.bat, and *.bat and Javabridge.jar the same directory) instead of double-click. After the dialog box is not selected, click "OK" directly. 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=NewJava (' Java.lang.System ');$s=NewJava ("java.lang.String", "Php-java-bridge config...<br><br>");Echo $s;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> ';$formatter=NewJava (' Java.text.SimpleDateFormat ', "eeee, MMMM dd, yyyy ' at ' H:mm:ss a zzzz");Print $formatter->format (NewJava (' java.util.Date ')). ' <br> '. ' <br> ';

    • Find the Javabridge.jar in Javabridge\web-inf\lib, unzip Javabridge.jar can find the Java directory in Javabridge\meta-inf, Complete copy of the Java directory with test.php directory, and then run: http://localhost/test.php, will have the following output, indicating that the Javabridge installation was successful

1.4. Calling a custom Java class in PHP

    • First build a test class Test.java:
 Public classTest {PrivateString name = ""; //Setter and Getter     Public voidsetName (String name) { This. Name =name; }      PublicString GetName () {return  This. Name; }     //addition     Public floatAddfloatNUM1,floatnum2) {       returnNUM1 +num2; }}

    • After writing the class, the Test.java compiler generates the class file and copies the Test.class to the C:\Program files\java\jre7\classes directory, because the Jre7 folder name may be different because of the version number. The JDK is installed by default without C:\Program files\java\jre7\classes and needs to be established manually.
    • The contents of the modified test.php are:
// Custom class Testing require_once ("Java/java.inc"); $test New // Generating Instances $test // The subsequent call is just like calling the class method in PHP Print "Call the GetName method of class test, the return value is:". $test->getname (). " <br> "; Print "Call Test's Add method with the return value:". $test->add (11.2, 15.7);

    • Again visit http://localhost/test.php, the page will output the following content:
    • On this east to make a day to get out, hey online find a lot of methods are not feasible, so every time to move the class to the JRE directory inconvenient, plus Java_require ("Test.jar"); The reference jar package reported this error: Warning:java_require () not supported anymore. <a href= "http://php-java-bridge.sourceforge.net/pjb/webapp.php>tomcat or jee hot deployment</a > instead in D:\wamp\www\java\Java.inc on line 1656 do not know how to make a direct reference jar package, students will also please advise Oh!

PHP calls the Java method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.