How does PHP call JAVA class libraries?

Source: Internet
Author: User

 

JAVA is a very powerful programming tool and its extension library is also very useful. This tutorial mainly describes how to use PHP to call a powerful JAVA class library (classes ). For your convenience, this tutorial will include JAVA installation and some basic examples.
Installation in Windows
Step 1: Install JDK. This is very easy. You only need to press enter to install JDK. Perform the following steps.
Add "PATH = % PATH %; C: jdk1.2.2in" to the AUTOEXEC. BAT file Under Win9x.
Add "; C: jdk1.2.2in" to the environment variable under NT.
This step is very important, so that PHP can correctly find the JAVA class to be called.
Step 2: Modify your PHP. ini file.
[Java]
Extension = php_java.dll
Java. library. path = c: webphp4extensions
Java. class. path = "c: webphp4extensionsjdk1.2.2php _ java. jar; c: myclasses"
In PHP. add extension = php_java.dll to INI, and set java in [java. class. path to point to php_java.jar. If you use the new JAVA class, you should also store the path. In this example, we use the c: myclasses directory.
Step 3: create the following PHP file in the test environment:
<? Php

$ System = new Java ("java. lang. System ");
Print "Java version =". $ system-> getProperty ("java. version"). "<br> ";
Print "Java vendor =". $ system-> getProperty ("java. vendor"). "<p> ";
Print "OS =". $ system-> getProperty ("OS. name ")."".
$ System-> getProperty ("OS. version"). "on ".
$ System-> getProperty ("OS. arch"). "<br> ";

$ Formatter = new Java ("java. text. SimpleDateFormat", "EEEE,
MMMM dd, yyyy at h: mm: ss a zzzz ");
Print $ formatter-> format (new Java ("java. util. Date "))."";

?>

If the installation is correct, you will see the following information:

Java version = 1.2.2
Java vendor = Sun Microsystems Inc.
OS = Windows 95 4.10 on x86
Wednesday, October 18,200 0 at 10:22:45 AM China Standard Time
In this way, we have successfully established a PHP runtime environment that can use the JAVA class, and we can start our next course.
Example 1: create and use your own JAVA class
It is very easy to create your own JAVA class. Create a new phptest. java file and place it in your java. class. path directory. The file content is as follows:

Public class phptest {
/**
* A sample of a class that can work with PHP
* NB: The whole class must be public to work,
* And of course the methods you wish to call
* Directly.
*
* Also note that from PHP the main method
* Will not 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 is run from the command line
* 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 need to compile this file and use the javac phptest. java command in the doscommand line.

To test this JAVA class using PHP, we create a phptest. PHP file with the following content:

<? 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 the warning: java. lang. ClassNotFoundException error, it means that your phptest. class file is not in your java. class. path directory.

Note that JAVA is a forced type language, but PHP is not. In this way, errors are easily caused when we integrate them. Therefore, when we pass variables to JAVA, you must specify the variable type correctly. For example: $ 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 very well!

 

 

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.