PHP calls Java class files

Source: Internet
Author: User

Recently in the toss PHP call Java class, online access to data, the final choice Javabridge, the first problem encountered is the Java_require () function introduced a custom Java packaged jar file, in the new version of Javabridge, the function has been discarded, Use will go wrong.

On-line search data, there are roughly two methods, pro-test feasible;

Method One

Download the php-java-bridge_6.2.1 War pack from http://php-java-bridge.sourceforge.net/pjb/index.php, Placing the war package under Tomcat's application directory, WebApps, launches tomcat and automatically resolves a folder with the same name, where Javabridge.jar and java.inc are needed.

In the program, be sure to remember the introduction of the Java.inc file, the format is require_once ("Http://localhost:8080/JavaBridge/java/Java.inc"); note that since Tomcat is now running, This can be requested by using HTTP, but by default PHP does not support links from non-local servers, so you need to modify the php.ini file to set these two values to be turned on:

1 allow_url_fopen = on2 allow_url_include = On (this property may be added)

The general test example would look like this:

1<?PHP2 require_once("Http://localhost:8080/JavaBridge/java/Java.inc");3 $system=NewJava (' Java.lang.System ' );4Java_set_file_encoding ("UTF-8");//format encoding to avoid garbled characters5 Print' Java version= '.$system->getproperty (' java.version '). ' <br> ' ; 6 Print' Java vendor= '.$system->getproperty (' Java.vendor '). ' <br> ' ; 7 Print' os= '.$system->getproperty (' Os.name '). ‘ ‘ .8 $system->getproperty (' os.version '). ' On '.9 $system->getproperty (' Os.arch '). ' <br> ' ; Ten $te=$system-getProperties (); One  Echo $te; A?>

This example will output the Java version number and a series of system constants if the run succeeds, which means that at least PHP calls the Java predefined system class and that there are no problems, the result will be similar (just a partial intercept):

To invoke a custom class, such as the test class, the source code is:

1  Public classTest2 {3     PrivateString name = "";4 5     //Setter and Getter6      Public voidsetName (String name)7     {8          This. Name =name;9     }Ten  One      PublicString getName () A     { -         return  This. Name; -     } the  -     //addition -      Public floatAddfloatNUM1,floatnum2) -     { +         returnNUM1 +num2; -     } +}

You need to use the Package command to package the compiled Test.class file into a Test.jar file, placed in the Java installation directory F:\javaSetup\jre8\lib\ext issued, The Javabridge.jar file does not need to be put in at the same time, because it is possible to have a referential relationship in Java.inc, to create an instance of the class through Java functions in the program $test=new Java (' Test '), and then to invoke the method in the same way as Java, as follows:

1 $test=new Java ("Test"); 2 $test->setname ("Haha, PHP calls Java Method! "); 3 Echo "Call the GetName method of class test, the return value is:". $test->getname (). " <br> "; 4 Echo "Call Test's Add method with the return value:". $test->add (11.2, 15.7). " <br> ";

Method Two

The second method does not need the entire war package, only the Javabridge.jar and Java.inc, the first still need to introduce java.inc files, theoretically where can be, as long as the path is correct. Then switch to the Javabridge.jar path at the command line, through the start Javaw-jar Javabridge.jar Run the jar package, this will pop up a selection box, generally choose the default (need to pay attention to port problems, avoid being occupied), run the following code, generally no problem:

1<?PHP2Require_once ("Java/java.inc");//Introducing Java.inc Files3 $system=NewJava (' Java.lang.System ' );4Java_set_file_encoding ("UTF-8");//format encoding to avoid garbled characters5 Print' Java version= '.$system->getproperty (' java.version '). ' <br> ' ; 6 Print' Java vendor= '.$system->getproperty (' Java.vendor '). ' <br> ' ; 7 Print' os= '.$system->getproperty (' Os.name '). ‘ ‘ .8 $system->getproperty (' os.version '). ' On '.9 $system->getproperty (' Os.arch '). ' <br> ' ; Ten $te=$system-getProperties (); One  Echo $te; A?>

If successful, it is possible to invoke the Java system class at this time.

To invoke a custom Java class, still take the previous Test.java as an example, package it into Test.jar, put it into F:\javaSetup\jre8\lib\ext (Java installation directory), If Javabridge.jar is already running, it needs to be restarted and then called as follows:

1 $test=new Java ("Test"); 2 $test->setname ("Haha, PHP calls Java Method! "); 3 Echo "Call the GetName method of class test, the return value is:". $test->getname (). " <br> "; 4 Echo "Call Test's Add method with the return value:". $test->add (11.2, 15.7). " <br> ";

There is a phenomenon to note that when invoking the Java System class output system variable, the output of the first method is significantly more than the second method.

Possible problems:

1. A reference to a single-file jar package may not be a big problem, but if you need many custom jars you might have problems

2. Is it still possible to do this without starting tomcat???

The experiment proves that, in the first way, this is not possible, the following error occurs, the error is the same as the second way of not enabling Javabridge.jar, which means that when you run Tomcat, the jar file runs with it.

However, when introducing the Java.inc file, the Inc file may not be placed under Tomcat's WebApps, in theory the file can be anywhere:

Require_once ("Http://localhost:8080/JavaBridge/java/Java.inc"), and Require_once ("Java/java.inc"), are equivalent.

PHP calls Java class files

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.