PHP calls respective Java (GO)

Source: Internet
Author: User
PHP calls its own Java (GO)
-----------------------------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

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 with the WinRAR open, to web-inf/lib/javabridge.jar the jar bag to copy Your PHP directory under the ext/.

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 off
Start 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:


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.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. Launch Apache, view http://localhost/demo/test.php in the browser

You will see the following information:

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 that can work with PHP
* Nb:the whole class must is public to work,
* And of course the methods you wish
* directly.
*
* Also Note that from PHP the Main method
* Won't is 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 are 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));
}
}
}
}


Into the jar and copy it to the D disk.



/demo/index2.php
Require_once ("Java/java.inc");

Java_require ("D://1.jar");

$myj = new Java ("Ttt.phptest");
echo "Test Results is " . $myj->test ("Hello World"). "";

$myj->foo = "A String Value";
echo "You had set Foo to " . $myj->foo. "
\ n ";
echo "My Java Method reports: " . $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 Dai or http://sourceforge.net/project/showfiles.php?group_id=117793 by yourself)


Php-java-bridge the downloaded back, the decompression box will have a javabridge.war, and then the same javabridge.war to decompress (win RAR can be solved)
After decompression, you can find the Java-x86-windows.dll from the CGI box in the Web-inf, and find the Lib in the Web-inf magazine Javabridge.jar

The Java-x86-windows.dll and Javabridge.jar are copied to the outer casing of PHP (I'm C:/appservphp/ext), and the Java-x86-windows.dll is changed to Php_java.dll

Modify php.ini Files

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 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"

Re-start 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
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



The second Java status is not running (this is because you don't have a javabridge.jar). If it becomes running <----Representative Javabridge.jar has been launched, it can be formally used Php-java-bridge

---Execute if not started:
Because it's not possible to activate Javabridge.jar every time you start a machine.

So we write a batch file with the following content

@echo off
Start Javaw-jar Javabridge.jar

Save it as a phpjavabridge.bat, in the same way as in the outer casing of PHP (this is c:appservphpext)

Set the file to the right path and put the built-in path into the startup (here is the c:documents and Settingsall users"Start menu program)

This way, each time you open the machine, you will automatically activate the Phpjavabridge.bat in the C:appservphpext.



------------------------------Simple Example

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

echo "Java version". $system->getproperty (' java.version '). "
";
echo "Maker". $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



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



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

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.

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

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



------------------creating and using 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 that can work with PHP
* Nb:the whole class must is public to work,
* And of course the methods you wish
* directly.
*
* Also Note that from PHP the Main method
* Won't is 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 are 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 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 = new Java ("Phptest");
echo "Test Results is" . $myj->test ("Hello World"). "";

$myj->foo = "A String Value";
echo "You had set Foo to" . $myj->foo. "
\ n ";
echo "My Java Method reports:" . $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.

  • 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.