PHP calls its own Java program methods and examples of detailed

Source: Internet
Author: User
This article mainly introduces PHP to call its own Java program, a more detailed analysis of PHP based on third-party jar package call Java program and use DLL dynamic link library file extension for Java calls two methods, the need for friends can refer to the following

Specific as follows:

The first to install the JDK this is needless to say, I installed the Java EE 5+jdk

1. Extract the downloaded php-java-bridge_5.2.2_j2ee.zip , there is a Javabridge.war directly open with WinRAR, to web-inf/lib/ Javabridge.jar the jar to the ext/of your PHP directory.

2. Open the war package with a Java folder in it and copy it all to your PHP project, such as/demo/java

3. The current version is Vmbridge, to PHP call Java class, the first to start the Javabridge,

Command line down with Java–jar Javabridge.jar or double-click Javabridge.jar, select the listening port in the popup window 8080

For later start-up convenience, I created a bat file under ext/the following content:

@echo Offstart Javaw-jar Javabridge.jar

After saving, double-click Launch will have a prompt box select Vmbridge port Default 8080, direct click OK on the line

4. The new test.php content under/demo/is as follows:

<?phprequire_once ("Java/java.inc"); header ("content-type:text/html; Charset=utf-8″);//Get instance of Java class Java.lang.System in php$system = new Java (' Java.lang.System '); $s = new Java ( "Java.lang.String", "Php-java-bridge config...<br><br>"); echo $s;//demonstrate property Accessprint ' Java Version= '. $system->getproperty (' java.version '). ' <br> ';p rint ' Java vendor= '. $system->getproperty (' Java.vendor '). ' <br> ';p rint ' os= '. $system->getproperty (' Os.name '). ' '. $system->getproperty (' os.version '). ' on '. $system->getproperty (' Os.arch '). ' <br> ';//Java.util.Date Example$formatter = new Java (' Java.text.SimpleDateFormat ', "eeee, MMMM dd, yyyy ' at ' h:mm:ss a zzzz");p rint $formatter-> ; Format (new Java (' java.util.Date '));? >

5. Launch Apache, view http://localhost/demo/test.php in the browser

You will see the following information:

Copy the Code code as follows:

Php-java-bridge config ...
Java version=1.6.0_10
Java Vendor=sun Microsystems Inc.
Os=windows Vista 6.0 on x86
Sunday, November at 4:31:49 China Standard Time

Custom jar:

Package Ttt;public class phptest{/** * A sample of a class The can work with PHP * nb:the Whole class must is public to Work, * and of course the methods-wish to call * directly. * * Also Note that from PHP the main method * won't be called */public String foo; /** * Takes A string and returns the result * or a msg saying your string was empty */public string test (string str) {I  F (Str.equals (")) {str =" Your string was empty. ";} return str; }/** * Whatisfoo () simply returns the value of the variable foo. */Public String Whatisfoo () {return ' foo is ' + foo;} /** * This is called if phptest are run from the command line with * something like * java phptest * or * java phptest hell  o There */public static void Main (String args[]) {phptest p = new Phptest ();   if (Args.length = = 0) {String arg = "";  System.out.println (P.test (ARG));    }else{for (int i = 0; i < args.length; i++) {String arg = args[i];   System.out.println (P.test (ARG)); }  } }}

Into the jar and copy it to the D disk.

/demo/index2.php

<?require_once ("Java/java.inc"); Java_require ("D://1.jar"); $myj = new Java ("Ttt.phptest"); echo "Test Results is <b> ". $myj->test ("Hello World"). "</b>"; $myj->foo = "A String Value"; echo "you had set Foo to <b>". $myj->foo. "</b><br>\n"; echo "My Java Method reports: <b>". $myj->whatisfoo (). "</b><br>\n";? >

View http://localhost/demo/index2.php in the browser

Method Two: Php_java.dll need to configure php.ini, the new version of Php-java-bridge no DLL file

First make sure your PHP and Apache server and JDK (or JRE) are all installed

Download Php-java-bridge on the Internet (find yourself a bit or http://sourceforge.net/project/showfiles.php?group_id=117793)

Will download the Php-java-bridge extract, unzip the folder will have a Javabridge.war, and then also this Javabridge.war decompression (win RAR can be solved)
After decompression can be found in the Web-inf folder in the CGI folder Java-x86-windows.dll, and Web-inf folder in the Lib folder found Javabridge.jar

Copy Java-x86-windows.dll and Javabridge.jar to PHP's plug-in folder (my side is c:/appservphp/ext) and change Java-x86-windows.dll to Php_java.dll

Modify PHP.ini Archive

If PHP.ini originally did not have the following content, please add, if the original has the following content, please modify it as follows [I am using a JDK]

Extension=php_java.dll

[Java];java.java = "C:\jdk1.6.0_13\bin\java" Java.class.path = "d:\php\ext\javabridge.jar;c:\myclasses" c \ MyClasses can be customized to store the Java files that you write java.java_home = "C:\jdk1.6.0_13\jre" java.library = "d:\jdk1.2.2\jre\bin\server\ Jvm.dll "Java.library.path =" D:\php\ext "

Restart Apache, view Phpinfo

Javajava support  Enabledjava bridge  3.0.8java.java_home  C:\jdk1.6.0_13java.java  C:\jdk1.6.0_13\ Bin\javajava.log_file  <stderr>java.log_level  No value (use backend ' s default level) Java.ext_java_ Compatibility  Offjava command  C:\jdk1.6.0_13\bin\java-djava.library.path=d:\php\ext-djava.class.path=d : \php\ext/javabridge.jar-djava.awt.headless=true Php.java.bridge.JavaBridge inet_local:0 2java status  Runningjava server  9267

Look at the penultimate Java status is not running (this is because you did not start Javabridge.jar). If it becomes running <--representative Javabridge.jar has been started, it can be used formally Php-java-bridge

If it does not start then execute:

Because it is not possible to manually start each boot Javabridge.jar

So we write a batch that reads as follows

@echo Offstart Javaw-jar Javabridge.jar

Save it as Phpjavabridge.bat and put it in the PHP plug-in folder (this is C:appserv/php/ext)

Set up a shortcut to the file, put the established shortcut into the startup (here is C:/Documents and settings/all users/"start"/function table/program start)

This will automatically start the Phpjavabridge.bat in the C:appservphpext folder after each boot.

Simple example

<? $system =new Java (' Java.lang.System '); echo "Java version". $system->getproperty (' java.version '). " <BR> ", echo" Issuer ". $system->getproperty (' Java.vendor ')." <BR> "; echo" Operating system version ". $system->getproperty (' Os.name ')." <BR> "; echo" Java version ". $system->getproperty (' os.version ')." <BR> "; echo" Java version ". $system->getproperty (' Os.arch ')." <BR> ";? >

Or find test.php,http://localhost/test.php in Php-java-bridge to see the effect

<?php$system=new java ("Java.lang.System");p rint "Java version=". $system->getproperty ("Java.version"). "< Br> ";? >

[Java]extension=php_java.dlljava.library.path=c:webphp4extensionsjava.class.path= "C: Webphp4extensionsjdk1.2.2php_java.jar;c:myclasses "

Add Extension=php_java.dll to the php.ini, and in [Java], set the Java.class.path so that it points to php_ Java.jar, if you use the new Java class, you should also deposit this path, in this example, we use the c:myclasses directory.

Test environment, create the following PHP file:

<?php$system = new Java ("Java.lang.System");p rint "Java version=". $system->getproperty ("Java.version"). "< Br>n ";p rint" Java vendor= ". $system->getproperty (" Java.vendor ")." <p>nn ";p rint" os= ". $system GetProperty ("Os.name"). " ". $system->getproperty (" Os.version ")." On ". $system->getproperty (" Os.arch ")." <br>n "; $formatter = new Java ("Java.text.SimpleDateFormat", "eeee,mmmm dd, yyyy ' at ' h:mm:ss a zzzz");p rint $formatter->format (New Java (" Java.util.Date "))." n ";? >

If you install it correctly, you will see the following information:

Copy the Code code as follows:

Java version=1.2.2
Java Vendor=sun Microsystems Inc.
Os=windows 4.10 on x86
Wednesday, October, at 10:22:45

Understanding how to call Java is important, the next step is to create our own Java files, let PHP call, Java file Java.class.path is very important

Create and use your own Java class [ note case ]

It's very easy to create your own JAVA classes. Create a new Phptest.java file and place it in your Java.class.path directory "c:\myclasses" with the following file contents:

public class phptest{/** * A sample of a class The can work with PHP * nb:the Whole class must is public to work, * and Of course the methods you wish to call * directly. * * Also Note that from PHP the main method * won't be called */public String foo; /** * Takes A string and returns the result * or a msg saying your string was empty */public string test (string str) {I  F (Str.equals (")) {str =" Your string was empty. ";} return str; }/** * Whatisfoo () simply returns the value of the variable foo. */Public String Whatisfoo () {return ' foo is ' + foo;} /** * This is called if phptest are run from the command line with * something like * java phptest * or * java phptest hell  o There */public static void Main (String args[]) {phptest p = new Phptest ();   if (Args.length = = 0) {String arg = "";  System.out.println (P.test (ARG));    }else{for (int i = 0; i < args.length; i++) {String arg = args[i];   System.out.println (P.test (ARG)); }  } }}

After creating this file, we will compile this file and use the Javac Phptest.java command on the DOS command line.

To test this JAVA class with PHP, we create a phptest.php file in the Web directory with the following content:

<?php$myj = new Java ("Phptest"); echo "Test Results is <b>". $myj->test ("Hello World"). "</b>"; $myj->foo = "A String Value"; echo "you had set Foo to <b>". $myj->foo. "</b><br>\n"; echo "My Java Method reports: <b>". $myj->whatisfoo (). "</b><br>\n";? >

If you get this warning message: java.lang.ClassNotFoundException error, this means your Phptest.class file is not in your Java.class.path directory.

Note that JAVA is a mandatory type language, and PHP is not, so that when we merge them, it is easy to lead to errors, so when we pass the variable to JAVA, we should correctly specify the type of the variable. such as: $myj->foo = (string) 12345678; or $myj->foo = "12345678″;

This is just a small example, you can create your own JAVA class and use PHP to call it nicely! The key is to understand the importance of the Java.class.path directory.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.