For details about how php calls its own java program and how php calls java. Php calls its own java program method details, php calls java detailed examples of this article describes how php calls its own java program implementation method. For your reference, the details are as follows: details on 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…
");echo $s;// demonstrate property accessprint 'Java version='.$system->getProperty('java.version').'
';print 'Java vendor=' .$system->getProperty('java.vendor').'
';print 'OS='.$system->getProperty('os.name').' '.$system->getProperty('os.version').' on '.$system->getProperty('os.arch').'
';// 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:
The 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 " . $myj->test("Hello World") . "";$myj->foo = "A String Value";echo "You have set foo to " . $myj->foo . "
\n";echo "My java method reports: " . $myj->whatisfoo() . "
\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
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 ')."
"; Echo" Issuer ". $ system-> getProperty ('Java. upload ')."
"; Echo" job system Version ". $ system-> getProperty ('OS. name ')."
"; Echo" java version ". $ system-> getProperty ('OS. version ')."
"; Echo" java version ". $ system-> getProperty ('OS. arch ')."
";?>
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")."
";?>
[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")."
n";print "Java vendor=".$system->getProperty("java.vendor")." nn";print "OS=".$system->getProperty("os.name")." ".$system->getProperty("os.version")." on ".$system->getProperty("os.arch")."
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:
The 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 " . $myj->test("Hello World") . "";$myj->foo = "A String Value";echo "You have set foo to " . $myj->foo . "
\n";echo "My java method reports: " . $myj->whatisfoo() . "
\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.