Php calls java from every child)

Source: Internet
Author: User
Php calls its own java (convert) --------------------------- recommended method: JavaBridge. jarhttp: // sourceforge.net/projects/php-java-bridgehttp://mi php calls its own java (convert)
----------------------------- Recommended method: JavaBridge. jar

Http://sourceforge.net/projects/php-java-bridge

Http://mirror.optus.net/sourceforge/p/ph/php-java-bridge/php-java-bridge_5.2.2_j2ee.zip

Jdk should be installed at the beginning. I installed java ee 5 + jdk.

1. extract the downloaded php-java-bridge_5.2.2_j2ee.zip, which has a JavaBridge. war opened directly with winrar, to the WEB-INF/lib/JavaBridge. jar to 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 off
Start 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:


Require_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 access
Print 'Java version = '. $ system-> getProperty ('Java. version ').'
';
Print 'Java vendor = '. $ system-> getProperty ('Java. upload ').'
';
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:

Php-java-bridge config...

Java version = 1.6.0 _ 10
Java vendor = Sun Microsystems Inc.
OS = Windows Vista 6.0 on x86
Sunday, July 22, 2008 at 4:31:49 pm China Standard 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
* 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 ". $ 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 files are available for the new php-java-bridge version.



First, make sure that your PHP and Apache servers and JDK (or JRE) are installed successfully.

On the net under the php-java-bridge (find your own wear or http://sourceforge.net/project/showfiles.php? Group_id = 117793)


The next php-java-bridge solution will be released. after the solution, there will be a JavaBridge in the database. war. war decoding worker (win rar)
After the solution, you can find the WEB-INF from the cgi information in the java-x86-windows.dll tray, and the lib information in the WEB-INF tray find JavaBridge. jar

Compile the java-x86-windows.dll and JavaBridge. jar libraries into the PHP foreign resource (I am here C:/AppServphp/ext) and change the java-x86-windows.dll to php_java.dll

Modify the php. ini example

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 to store self-written JAVA files.
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

Java
Java support Enabled
Java bridge 3.0.8
Java. java_home C: \ jdk1.6.0 _ 13
Java. java C: \ jdk1.6.0 _ 13 \ bin \ java
Java. log_file
Java. log_level no value (use backend's default level)
Java. ext_java_compatibility Off
Java 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 2
Java status running
Java server 9267



Check whether zookeeper of the Second java status is not running (this is because you have not modified JavaBridge. jar ). If it becomes running <---- indicates that JavaBridge. jar has been triggered, php-java-bridge can be officially used.

---- If it is not started, run:
Because it is impossible to manually update JavaBridge. jar on the machine every time.

Therefore, we have a batch of updates with the following content:

@ Echo off
Start javaw-jar JavaBridge. jar

Save it as phpJavaBridge. bat, and put it in the PHP external resource tray (here C: AppServphpext)

Set up a software release and place the created software in the notebook (C: Documents and SettingsAll Users start menu)

In this case, the C: AppServphpext file in the phpJavaBridge. bat will be automatically triggered every time the server is turned on.



------------------------------ Simple example

$ System = new Java ('Java. lang. system ');

Echo "java version". $ system-> getProperty ('Java. version ')."
";
Echo "producer". $ system-> getProperty ('Java. upload ')."
";
Echo "operating 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.



---------------------



$ System = new Java ("java. lang. System ");
Print "Java version =". $ system-> getProperty ("java. version ")."
";
?>


-------------------------------
[Java]
Extension = PHP_java.dll
Java. library. path = c: webPHP4extensions
Java. 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.

------------------------------------------ Test environment, create the following php file:
 

$ 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:

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
* 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:


$ Myj = new Java ("phptest ");
Echo "Test Results are". $ Myj-> test (" Hello World ")."";

$ Myj-> foo = "A String Value ";
Echo "You have set foo". $ 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.

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.