Detailed explanation of how php calls its own java program and how php calls java
This example describes how php calls its own java program. We will share this with you for your reference. The details are as follows:
Jdk should be installed at the beginning. I installed java ee 5 + jdk.
1.decompress the downloaded php-java-bridge_5.2.2_j2ee.zip file.JavaBridge. warOpen it directly with winrar, to the WEB-INF/lib/JavaBridge. jar copy this jar package to your php Directory ext.
2. Open the war package, which contains a java folder, and copy all of it to your PHP project, such as/demo/java
3. The current version is VMBridge. To use php to call the java class, you must first start JavaBridge,
Call java-jar JavaBridge. jar in the command line or double-click JavaBridge. jar. In the displayed window, select the listening port 8080.
For later startup convenience, I created a bat file in ext/with the following content:
@echo offstart javaw -jar JavaBridge.jar
After saving, double-click to start. A prompt box is displayed, selecting vmbridge port default 8080. Click OK.
4. Create test. php In/demo/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>';print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';print '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");print $formatter->format(new Java('java.util.Date'));?>
5. Start apache and check http: // localhost/demo/test. php in the browser.
The following information is displayed:
Copy codeThe Code is 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, October 11, 2008 at 4:31:49 PM China Standard Huai time
Custom JAR:
package ttt;public class phptest{ /** * A sample of a class that can work with PHP * NB: The whole class must be public to work, * and of course the methods you wish to call * directly. * * Also note that from PHP the main method * will not 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) { if(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 is run from the command line with * something like * java phptest * or * java phptest hello 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)); } } }}
Generate a JAR file and copy it to drive D.
/Demo/index2.php
<?require_once("java/Java.inc");java_require("D://1.jar");$myj = new Java("ttt.phptest");echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";$myj->foo = "A String Value";echo "You have 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 2: php. ini needs to be configured for php_java.dll. No dll file exists in the new version of php-java-bridge.
First, make sure that your PHP and Apache server and JDK (or JRE) have been installed.
Download php-java-bridge online (find your own wear point or http://sourceforge.net/project/showfiles.php? Group_id = 117793)
Decompress the downloaded php-java-bridge file. After decompression, A JavaBridge. war file is displayed in the folder, and then decompress the JavaBridge. war file (win rar)
After decompression, find the WEB-INF from the cgi folder in the java-x86-windows.dll folder, and find JavaBridge. jar in the lib folder in the WEB-INF folder
Compile the java-x86-windows.dll and JavaBridge. jar packages to the PHP plug-in folder (my side is C:/AppServphp/ext), and change the java-x86-windows.dll to php_java.dll
Modify the php. ini file
If php. ini does not have the following content, add it by yourself. If the following content already exists, change it to the following [JDK is used]
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, used to store your own written JAVA file 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 and 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
Check whether the status of the penultimate java status item is not running (this is because JavaBridge. jar is not started ). If it becomes running <-- indicates that JavaBridge. jar has been started, php-java-bridge can be officially used.
If it is not started, run:
Because it is impossible to manually start JavaBridge. jar
So let's write a batch process with the following content:
@echo offstart javaw -jar JavaBridge.jar
Save it as phpJavaBridge. bat and put it in the PHP plug-in folder (Here C: AppServ/php/ext)
Set a shortcut for this file and put the created shortcut into startup (here is C:/Documents and Settings/All Users/"start"/menu/program startup)
In this way, the phpJavaBridge. bat in the C: AppServphpext folder will be automatically started 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 "job system version ". $ system-> getProperty ('OS. name '). "<BR>"; echo "java version ". $ system-> getProperty ('OS. version '). "<BR>"; echo "java version ". $ system-> getProperty ('OS. arch '). "<BR>";?>
You can also find test. php, http: // localhost/test. php in php-java-bridge to check the effect.
<?php$system=new Java("java.lang.System");print "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"
In PHP. add extension = PHP_java.dll to INI, and set java in [java. class. path to point to PHP_java.jar. If you use the new JAVA class, you should also store the path. In this example, we use the c: myclasses directory.
In the test environment, create the following PHP file:
<?php$system = new Java("java.lang.System");print "Java version=".$system->getProperty("java.version")." <br>n";print "Java vendor=".$system->getProperty("java.vendor")." <p>nn";print "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");print $formatter->format(new Java("java.util.Date"))."n";?>
If the installation is correct, you will see the following information:
Copy codeThe Code is as follows: Java version = 1.2.2
Java vendor = Sun Microsystems Inc.
OS = Windows 95 4.10 on x86
Wednesday, October 18,200 0 at 10:22:45 AM China Standard Time
It is very important to understand how to call JAVA. Next, we need to create our own JAVA file for PHP to call. The JAVA. class. path of the java file is very important.
Create and use your own JAVA class [case sensitive]
It is very easy to create your own JAVA class. Create a new phptest. java file and place it under your java. class. path directory [c: \ myclasses]. The file content is as follows:
public class phptest{ /** * A sample of a class that can work with PHP * NB: The whole class must be public to work, * and of course the methods you wish to call * directly. * * Also note that from PHP the main method * will not 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) { if(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 is run from the command line with * something like * java phptest * or * java phptest hello 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 need to compile this file and use the javac phptest. java command in the DOS command line.
To test this JAVA class using PHP, create a phptest. php file in the web directory. The content is as follows:
<?php$myj = new Java("phptest");echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";$myj->foo = "A String Value";echo "You have set foo to <b>" . $myj->foo . "</b><br>\n";echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>\n";?>
If you get the warning: java. lang. ClassNotFoundException error, it means that your phptest. class file is not in your java. class. path directory.
Note that JAVA is a forced type language, but PHP is not. In this way, errors are easily caused when we integrate them. Therefore, when we pass variables to JAVA, you must specify the variable type correctly. For example: $ 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 very well! The key lies in understanding the importance of the java. class. path directory.