Configuring the Java Environment for PHP
Java's powerful is well known, PHP's simple and easy to use is not put in doubt, but occasionally encounter PHP can not solve the problem-mainly certificate certification, processing and so on. So someone came up with a solution that would allow PHP to call Java classes.
There are a lot of articles on the web about how to use PHP to tune Java, but there is no shortage of *.dll articles. Everyone in PHP knows that *.dll files can only be used under Windows, which is contrary to PHP's cross-platform principles. Therefore, it is necessary to study a scheme that does not require any. dll files, and after a period of testing, we will summarize the experience.
Required files: (can download http://php-java-bridge.sourceforge.net/pjb/from its official website)
javabridge.jar--The core Java application package used to build the Web server interface.
java.inc--is a method package for PHP language calls, mainly Java classes.
Specific use:
Run Javabridge.jar first to start the Web server interface, run the command in the directory where Javabridge.jar is located: Java-jar Javabridge.jar servlet_local:8080. Where Java is not in the environment variable path, use absolute path. (If the Windows platform is associated with a jar file to JAVAW you can double-click Javabridge.jar, select servlet_local:8080 and click OK; the Linux platform can be used before the command nohup the program can be run continuously ; 8080 is a common port and can be set to a different port).
The next step is to write the PHP code:
Define ("Java_debug", true); Debug settings?
Define ("Java_hosts", "127.0.0.1:8080"); Set the Javabridge listening port, if the port on which the Javabridge.jar setting is turned on is not 8080, you can change it by this statement
Require_once ("Java/java.inc"); PHP calls Java interface, path problems need to be noted
$here =realpath (dirname ($_server["Script_filename"));
Java_set_library_path ($here. Path_separator. '. '); Set the Java development package (class or JAR file) path, with multiple paths separated by Path_separator to ensure cross-leveling support.
java_set_file_encoding ("GBK");????? Set the Java encoding. No other coding has been tried, and no further research has been done on how to use other encodings.
In front of the configuration environment, the following starts the real call:
$system = new Java ("Java.lang.System");//Initializes the class under Java, the main operation is to create an instance of the Java class, the first parameter of the Java class is the Java-developed class name contains the package path, The path represents the format of the imported package in Java. If a class under Java needs to use a constructor, you can use the second parameter.
Print "Java version=". $system->getproperty ("Java.version"). "\ n";
Print "Java vendor=". $system->getproperty ("Java.vendor"). "\ n";
Print "os=". $system->getproperty ("Os.name"). " ".? $system->getproperty ("Os.version"). "On". $system->getproperty ("Os.arch"). "\ n";
?>
To invoke a class that you have developed, the class must be of the public type. The method to invoke must also be of the public type.
such as: Ta.java
public class Ta
{
? public static void Main (string[] args)
? {
?? System.out.println ("Hello world!");
?}
? public String ts ()
? {
?? Return "from TS";
?}
}
in PHP
$ta? = new Java ("Ta");
Echo java_values ($ta->ts ());//output "from TS"
We recommend that you use the Java_values function to handle the values returned from instances of the Java class.
------------------------------------------------------
Regardless of 3721, run the following code once again (java.php):
$java = new Java ("Java.lang.System");
echo $java->getproperty ("Os.name");
?>
The resulting results are:
Fatal Error:cannot Instantiate non-existent Class:java in c:\apache\htdocs\java.php on line 3
This is of course the result of Ah, hehe. Since PHP calls Java, it logically requires three things: the first is PHP's own "receiver",
The second is the Java Virtual machine, and the third is of course the default Java classes.
Only if the above preparations are complete, can we use java! in PHP Here are some of the procedures I configured.
Locate the php.ini file, and my php.ini file is under c:\apache\php (other people may be under c:\winit or somewhere else).
I opened php.ini with notepad and searched for the letters in Java.
First, remove the semicolon in front of the Extension=php_java.dll. Run java.php if the "cannot instantiate ..." does not appear
Error, the first step is successful.
I run the result is to get
Fatal error:unable to load Java Library C:\jdk\jre\bin\hotspot\jvm.dll, Error: The specified module could not be found. In c:\apache\htdocs\java.php on line 3
I found it; java.library = C:\jdk\jre\bin\hotspot\jvm.dll, remove the semicolon from the front of the java.library and change it to
Java.library = C:\jdk1.3\jre\bin\hotspot\jvm.dll
The results are as follows:
Fatal Error:java.lang.noclassdeffounderror:net/php/reflect in c:\apache\htdocs\java.php on line 3
Finally find Java.class.path, complete the modification. The resulting results are:
Windows 2000
Haha, the configuration was successful!
My configuration is as follows:
[Java]
Extension=php_java.dll
Java.library = C:\jdk1.3\jre\bin\hotspot\jvm.dll
Java.class.path =. \java\php_java.jar
Java.library.path =. \extensions
Java.home = c:\jdk1.3
Description: Java.library.path must be the path to the directory where the Php_java.dll resides.
To sum up, PHP to call Java, logically see the need for three things: the first is PHP's own "receiver", here is called Php_java.dll.
The second is the Java Virtual machine, which is called Jvm.dll. The third, of course, is the default Java class, which is Php_java.jar.
So specific to the configuration above, these three things corresponding to the extension,java.library and Java.class.path respectively.
What if you also want to use a custom Java class? The answer is that you can modify Java.class.path,
For example: java.class.path= ". \java\php_java.jar;c:\myclass". Note that you have quotation marks and are separated by semicolons. One of the MyClass is you
The path to the directory where the "Custom Java class" is placed. When you use Java classes in PHP, the PHP sniffer is
Look for Php_java.jar and your MyClass directory below.
Finally, give me another "complete and full" configuration:
[Java]
Extension=php_java.dll
Java.library = C:\jdk1.3\jre\bin\hotspot\jvm.dll
Java.class.path = ". \java\php_java.jar;c:\myclass"
; Java.library.path =. \extensions
; java.home = c:\jdk1.3
If you remove the semicolon in front of the Java.library.path, then it must have the same value as Extension_dir, and here are the. \extensions.
Because Extension_dir is the path to the directory where all PHP extensions reside, Java.library.path is the path to the directory where Php_java.dll resides. So
will have such a request. So logically, Java.library.path is superfluous. and java.home it off so it can run successfully, so
is superfluous. Because the underlying reason is that the phrase "logically requires and requires only three"!
In this way, my configuration is "complete and full".
"Complete" means: With the above configuration, is completely enough; "full" means that the above configuration does not have any excess ingredients.
"Complete and full" means that I do not have a lot of this configuration, just right, I can no longer find a better configuration than my plan.
-Haha, summer is coming, everyone to buy my melon ah.