Configure the java environment for php

Source: Internet
Author: User
It is well known that java is powerful in configuring the JAVA environment for php, and PHP is easy to use, but sometimes PHP cannot solve the problem-mainly certificate authentication and processing. So someone found a solution for PHP to call JAVA classes. There are a lot of questions on how to use PHP to configure the java environment for php on the Internet

JAVA is widely known for its powerful functions. PHP is easy to use and not to be used. However, it may occasionally encounter problems that PHP cannot solve-mainly certificate authentication and processing. So someone found a solution for PHP to call JAVA classes.

There are many articles on how to use PHP to call JAVA on the Internet, but many of them need to use *. dll. PHP users know that *. dll files can only be used in windows, which is different from PHP cross-platform principles. Therefore, it is necessary to study the solution that does not require any. dll files. after a period of testing, we will summarize the experience.

Required files: (you can download the http://php-java-bridge.sourceforge.net/pjb/ from its official website)

JavaBridge. jar -- core JAVA application package used to build WEB server interfaces.

Java. inc is a method package called by the PHP language. it is mainly a Java class.

Specific usage:

First, run JavaBridge. jar to start the WEB server interface and run the command: java-jar JavaBridge. jar SERVLET_LOCAL: 8080 in the directory where JavaBridge. jar is located. If java is not included in the PATH of the environment variable, use the absolute PATH. (If a jar file is associated with javaw on windows, double-click JavaBridge. jar, select SERVLET_LOCAL: 8080, and click OK. on the Linux platform, you can use the nohup program to run continuously before the command. 8080 is a common port and can be set to another port ).

Then you can 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 set for javabridge. jar is not 8080, use this statement to change

Require_once ("java/Java. inc"); // php calls the java interface. Note the path problem.
$ Here = realpath (dirname ($ _ SERVER ["SCRIPT_FILENAME"]);
Java_set_library_path ($ here. PATH_SEPARATOR. '.'); // Set the java Development Kit (class or jar file) path. multiple paths are separated by PATH_SEPARATOR to ensure cross-plane support.
Java_set_file_encoding ("GBK ");????? // Set JAVA encoding. I have not tried other codes, nor have I studied how to use other codes.

// Configure the environment and call the environment as follows:

$ System = new Java ("java. lang. system "); // initialize the class in JAVA, the main operation is to create an instance of the Java class, the first parameter of the Java class is the name of the class developed by JAVA contains the package path, the path indicates the format of the imported package in JAVA. If the class in JAVA needs 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 call a self-developed class, the class must be of the public type. The method to be called must also be public.

For example, 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 process the values returned from Java class instances.

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

Regardless of 3721, run the following code first (java. php ):
$ Java = new java ("java. lang. System ");
Echo $ java-> getProperty ("OS. name ");
?>
The result is:
Fatal error: Cannot instantiate non-existent class: java in c: \ apache \ htdocs \ java. php on line 3

This is of course the result. Because php needs to call java, three things are required logically: first, php's own "receiver ",
The second is the java virtual machine, and the third is the default java class.
Only after the above preparations are completed can we use java in php! The following are some of my configuration processes.

Find the php. ini file. My php. ini file is under c: \ apache \ php (others may be under c: \ winit or somewhere else ).
I used notepad to open php. ini and search for the java letters.
Remove the semicolon before extension = php_java.dll. Run java. php. if "Cannot instantiate..." is not displayed
The first step is successful.

The result I run is
Fatal error: Unable to load Java Library c: \ jdk \ jre \ bin \ hotspot \ jvm. dll, error: the specified module cannot be found. In c: \ apache \ htdocs \ java. php on line 3

I found; java. library = c: \ jdk \ jre \ bin \ hotspot \ jvm. dll, remove the semicolon before java. library, and change it
Java. library = c: \ jdk1.3 \ jre \ bin \ hotspot \ jvm. dll

The result is as follows:
Fatal error: java. lang. NoClassDefFoundError: net/php/reflect in c: \ apache \ htdocs \ java. php on line 3

Finally, find java. class. path to complete the modification. The result is:
Windows 2000

Haha, the configuration is 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
Note: java. library. path must be the path of the directory where php_java.dll is located.

To sum up, php needs to call java. logically, there are three things: first, php's own "receiver", which is called php_java.dll.
The second is the java virtual machine, which is called jvm. dll. The third is of course some default java classes. here is php_java.jar.

Specifically, the three items correspond to extension, java. library, and java. class. path.
What if you want to use a custom java class? The answer is that you can modify java. class. path in this way,
For example, java. class. path = ". \ java \ php_java.jar; c: \ myclass ". Note that quotation marks must be provided and separated by semicolons. Myclass is yours.
Path of the "Custom java class" directory. When you use java classes in php, the php desorption is
Search for php_java.jar and your myclass directory.

Finally, I provide another "complete and sufficient" 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 before java. library. path, it must have the same value as extension_dir, which is. \ extensions.
Because extension_dir is the directory path of all php extension libraries, and java. library. path is the directory path of php_java.dll. So
This is the case. Therefore, logically, java. library. path is redundant. Java. home can still run successfully by commenting it out, so
It is also redundant. The root cause is that it is still the sentence "logically required and only needs three "!

In this way, my configuration is "complete and complete.
"Complete" means that the above configuration is enough. "full" means that the above configuration does not have any redundant components.
"Complete and sufficient" means that I have not many such configurations, which are just right. I can no longer find a better configuration solution than me.
-- Haha, summer is coming. everyone will buy my melons.

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.