PHP calls its own Java program in a detailed way, PHP calls Java detailed
The example in this article describes how PHP calls its own Java program implementation. Share to everyone for your reference, 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 ...
';//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
" . $myj->test ("Hello World"). "" . $myj->foo. "
" . $myj->whatisfoo (). "
\ 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
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 '). "
"Echo" Issuer ". $system->getproperty (' Java.vendor ')."
"; echo" operating system version. $system->getproperty (' Os.name '). "
"; echo" Java version ". $system->getproperty (' os.version ')."
"; echo" Java version ". $system->getproperty (' Os.arch ')."
";? >
Or find test.php,http://localhost/test.php in Php-java-bridge to see the effect
";? >
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:
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:
" . $myj->test ("Hello World"). "" . $myj->foo. "
" . $myj->whatisfoo (). "
\ 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.
More about PHP related content readers can view this site topic: "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Math Skills Summary, "PHP Regular Expression Usage summary", "PHP Operations and Operator Usage Summary", "PHP string Usage Summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1127921.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127921.html techarticle PHP calls its own Java program in a detailed way, PHP call Java Detailed example of this article describes the PHP call their own Java program implementation method. Share to everyone for your reference, as follows: The most open ...