Windows installation configuration php-java-bridge,php calls its own Java file ____php

Source: Internet
Author: User
Tags configuration php

----------------------------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 thing to install JDK this is needless to say, I installed Java EE 5+jdk

1. The download of the php-java-bridge_5.2.2_j2ee.zip extract out, there is a Javabridge.war directly with WinRAR open, to web-inf/lib/javabridge.jar the jar to the bag to the Your PHP directory under the ext/.

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:

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 afternoon 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 to work,
* And of course the methods you wish to call
* directly.
*
* Also Note this from PHP's Main method
* Won't 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 be 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 (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 and completed

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


To decompress the downloaded Php-java-bridge, to decompress the data box will have a javabridge.war, and then the same Javabridge.war decompression (win RAR can be solved)
The decompression can be found in the Java-x86-windows.dll from the CGI data cartridge in the Web-inf, and in the Lib information box in the Web-inf data box Javabridge.jar

Copy Java-x86-windows.dll and Javabridge.jar into PHP's outer-box (I'm c:/appservphp/ext here) and convert Java-x86-windows.dll to Php_ Java.dll

Modify the php.ini file

If PHP.ini 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 can be customized to store your own 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, 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



See if the second Java status State is not running (this is because you didn't activate Javabridge.jar). If it becomes running <----Representative Javabridge.jar has been launched, it can be formally used Php-java-bridge

----If no boot is performed:
Because it's impossible to start the Javabridge.jar every time you open a machine.

So we write a batch file with the following contents

@echo off
Start Javaw-jar Javabridge.jar

Save it as a phpjavabridge.bat, and put it in the PHP's outer box (this is C:appservphpext)

Create a shortcut and put the established path in the open (here is the c:documents and Settingsall users"start" feature table assembly activation)

In this way, after each drive, it will automatically activate the Phpjavabridge.bat in the C:appservphpext data box.



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

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

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

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 sample of a class that can work with PHP
* Nb:the whole class must to work,
* And of course the methods you wish to call
* directly.
*
* Also Note this from PHP's Main method
* Won't 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 be 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 (ARG) (p.test);
}else{
for (int i = 0; i < args.length; i++) {
String arg = args[i];
System.out.println (ARG) (p.test);
}
}
}
}

   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.

Php-java-bridge_3.0.8_j2ee.zip (7 MB) Download number: 236

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.