PHP calls its own Java program method to explain _php skills

Source: Internet
Author: User
Tags php programming php regular expression

This article is an example of how PHP invokes its own Java program implementation. Share to everyone for your reference, specific as follows:

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

1. Extract the downloaded php-java-bridge_5.2.2_j2ee.zip , which has 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 pack, which has a Java folder, and copy all of it to your PHP project, such as/demo/java

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

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

In order to start at a later convenience, I created a new bat file under ext/as follows:

@echo off
start Javaw-jar Javabridge.jar

After saving, double-click Startup will have a prompt box to choose Vmbridge Port default 8080, directly click OK on the line

4. Under/demo/, the new test.php content is as follows:

<?php
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...<br><br>");
echo $s;
Demonstrate property access
print ' 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, view http://localhost/demo/test.php in the browser

You will see the following information:

Copy 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, 2008 at 4:31:49 in the afternoon Chinese standard Huai time

Custom jar:

Package TTT;  public class phptest{/** * A class This can work with PHP * nb:the Whole class must is public to work, *
 The course the methods you wish to call * directly.
 * Also Note this from PHP "Main method *" is not to be called/public String foo; /** * takes a string and returns the result * or a msg saying your string is 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; }/** * is called if phptest are run from the command line with * something like * java phptest * or * java PHPT
  EST Hello there */public static void Main (String args[]) {phptest p = new Phptest ();
   if (Args.length = = 0) {String arg = ' ";
  System.out.println (ARG) (p.test);
    }else{for (int i = 0; i < args.length; i++) {String arg = args[i]; SysteM.out.println (ARG) (p.test);

 }
  }
 }
}

Born into a jar, copy to D disk.

/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 Two: Php_java.dll need to configure php.ini, the new version of Php-java-bridge does not have DLL files

First make sure that your PHP and Apache servers and JDK (or JRE) have been installed to complete

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

Will download the back of the Php-java-bridge decompression, after decompression folder will have a Javabridge.war, and then the same Javabridge.war uncompressed (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 to find the Lib folder Javabridge.jar

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

Modify php.ini File

If PHP.ini originally did not have the following content, please add, if the original has the following content, please modify to the following [I use the 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 customizable for storing your own 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, 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  <stderr>
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

Look at the second-lowest state of 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 officially used Php-java-bridge

If no startup is performed:

Because it's not possible to manually start the Javabridge.jar every time you boot

So we write a batch that reads as follows

@echo off
start Javaw-jar Javabridge.jar

Save it as a phpjavabridge.bat, also put it in the PHP folder (this is C:appserv/php/ext)

Make a shortcut to the file and put the shortcut into the startup (this is C:/Documents and settings/all users/"start"/function table/program startup)

As a result, each time after the boot will automatically start the C:appservphpext folder in the Phpjavabridge.bat

Simple example

?
$system =new Java (' Java.lang.System ');
echo "Java version". $system->getproperty (' java.version '). " <BR> ";
echo "publisher". $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 the test.php,http://localhost/test.php view effect in Php-java-bridge

<?php
$system =new Java ("Java.lang.System");
Print "Java version=". $system->getproperty ("Java.version"). "<br>";
? >

[Java]
Extension=php_java.dll
java.library.path=c:webphp4extensions
java.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, and in this case we use the C:myclasses directory.

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 you install it correctly, you will see the following information:

Copy 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 AM-Standard time

It is important to understand how Java is invoked, and the next step is to create our own Java files that PHP can invoke, and the Java.class.path of Java files is important

Create and use your own Java class [ note case ]

It is easy to create your own JAVA class. Create a new Phptest.java file and place it in your Java.class.path directory "c:\myclasses", which reads as follows:

public class phptest{/** * A class This 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 this from PHP "Main method *" is not to be called/public String foo; /** * takes a string and returns the result * or a msg saying your string is 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; }/** * is called if phptest are run from the command line with * something like * java phptest * or * java PHPT
  EST Hello there */public static void Main (String args[]) {phptest p = new Phptest ();
   if (Args.length = = 0) {String arg = ' ";
  System.out.println (ARG) (p.test);
    }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 Javac Phptest.java this command at the DOS command line.

To test this JAVA class using PHP, we create a phptest.php file under the Web directory, which reads 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 such a warning message: java.lang.ClassNotFoundException error, this means that 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 cause errors, so when we are passing variables to JAVA, we need to correctly specify the type of the variable. such as: $myj->foo = (string) 12345678; or $myj->foo = "12345678″;

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

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.