How to invoke a powerful Java class library using PHP

Source: Internet
Author: User
Tags command line empty ini php file string variable
Java is a very powerful programming tool, its extension library is also very useful, this tutorial, mainly on how to use PHP invoke powerful Java class library (classes). To facilitate your study, this tutorial will include Java installation and some basic examples.

Installation under Windows

Step One: Install the JDK, it's very easy, you just have to install it all the way to the carriage. Then do the following steps.
Add under Win9x: "path=%path%; C:\jdk1.2.2\bin "to the Autoexec.bat file
Add under NT "; C:\jdk1.2.2\bin "to the environment variable.

This step is very important so that PHP can correctly find the Java class that needs to be invoked.

Step Two: Modify your php.ini file.
[Java]
Extension=php_java.dll
Java.library.path=c:\web\php4\extensions\
Java.class.path= "C:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses"

Add Extension=php_java.dll to PHP.ini
And in [Java], set the Java.class.path to point to Php_java.jar, and if you use the new Java class, you should also deposit this path, and in this example we use the C:\myclasses directory.

Step three: Test the 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>\n\n";
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

In this way, we have successfully built a PHP runtime environment that can use Java classes, and we can start the course we're going to take.
Example 1: Creating and using your own Java class
It is easy to create your own Java class. Create a new Phptest.java file and place it in your Java.class.path directory, 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 that 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!

Experience: This is the Purple Moon, the man provided to me, I have previously configured in PHP JDK, but may be a lower PHP version of the reason, so the configuration process does not appear any errors, if you are in the high version of PHP configuration when you need to pay attention to:
1. Path problem: Java.class.path = "E:\php4java\php_java.jar;e:\hb\php3\java\class" (your class must be placed under class (in this case))
2.php new version of a separate directory, storage Php_java.jar,php_java.jar in the Java directory, not extensions inside, please note
3. In the high version of the php.ini, do not add Extension=php_java.dll, I (Purple Moon) began to add, but do not load the relevant library (library?) ), and then added; just fine (= = = = = =)
The experience of the Purple Moon Man: Do not believe too much of the previous information, what more think, more their own groping, after all things update too fast!! Not that I don't understand, but that the world is changing too fast. :)
I am here to tidy up, I hope that the configuration of PHP support Java comrades can help, but also please Violet Moon brother forgive me!!


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.