Example of java call implemented by JavaBridge in php

Source: Internet
Author: User

1. Execute the jar file in java-jar/home/lichun/workspace/JavaBridge. jar (the premise is that the java environment has been configured in linux)

2. Write your own java class

The code is as follows: Copy code
Package com. phpjava;
 
Public class Test {
Private String name = "";
   
// Setter and getter
Public void setName (String name)
     {
This. name = name;
     }
   
Public String getName ()
     {
Return this. name;
     }
   
// Addition
Public float add (float num1, float num2)
     {
Return num1 + num2;
     }
}

Into jar. Put it in the directory with jdk configured/usr/lib/jvm/jdk1.7.0 _ 51/jre/lib/ext
3php program

The code is as follows: Copy code
<? Php
       
Require_once ("Java. inc"); // required configuration file
Java_require ("Test. jar"); // reference the contained jar package
       
$ Test = new Java ("com. phpjava. Test"); // Generate an instance
$ Test-> setName ("haha, PHP calls the JAVA method! "); // The subsequent call is the same as the class method called in php.
       
Echo "call the getName method of the Test class. The returned value is:". $ test-> getName (). "<br> ";
Echo "call the add Method of Test and return value:". $ test-> add (11.2, 15.7 );
?>

Browser call output:

Call the getName method of the Test class. The returned value is Haha. PHP calls the JAVA method!

Call the add Method of Test. The returned value is 26.9.

Let's look at another example.

<? Php
// Define ("JAVA_DEBUG", true); // debug settings
Define ("JAVA_HOSTS", "127.0.0.1: 8080"); // Set the javabridge listening port. If the port set for javabridge. jar is not 8080, use this statement to change

Require_once ("java/Java. inc"); // php calls the java interface. Note the path problem.
$ Here = realpath (dirname ($ _ SERVER ["SCRIPT_FILENAME"]);
Java_set_library_path ($ here. PATH_SEPARATOR. '.'); // Set the java Development Kit (class or jar file) path. Multiple paths are separated by PATH_SEPARATOR to ensure cross-plane support.
Java_set_file_encoding ("GBK"); // sets the JAVA encoding. I have not tried other codes, nor have I studied how to use other codes.

// Configure the environment and call the environment as follows:

$ System = new Java ("java. lang. system "); // initialize the class in JAVA, the main operation is to create an instance of the Java class, the first parameter of the Java class is the name of the class developed by JAVA contains the package path, the path indicates the format of the imported package in JAVA. If the class in JAVA needs a constructor, you can use the second parameter.
Print "Java version =". $ system-> getProperty ("java. version"). "/n ";
Print "Java vendor =". $ system-> getProperty ("java. vendor"). "/n ";
Print "OS = ". $ system-> getProperty ("OS. name "). "". $ system-> getProperty ("OS. version "). "on ". $ system-> getProperty ("OS. arch "). "/n ";

?>

To call a self-developed class, the class must be of the public type. The method to be called must also be public.

For example, ta. java

Public class ta
{
Public static void main (String [] args)
{
System. out. println ("Hello World! ");
}
Public String ts ()
{
Return "from ts ";
}
}

In PHP


$ Ta = new java ("ta ");

Echo java_values ($ ta-> ts (); // output "from ts"

// We recommend that you use the java_values function to process the values returned from Java class instances.

 

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.