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. Install JDK in windows. you only need to press enter to install JAVA. it is a very powerful programming tool and its extension Library is also very useful, this tutorial 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.
Add extension = php_java.dll to PHP. INI.
In [java], set 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
$ 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!
Example 2: Use Xalan 1.2 to convert XML using XSLT
As the second example, we use the Xalan-java XSLT engine, which comes from the apache xml project. using this program, we can use XSL to convert XML source files. This greatly facilitates document processing and content management.
Before starting, we need to put the xerces. jar and xalan. jar files under the java. class. path Directory (these two files are included in Xalan-Java 1.2 and can be downloaded from xml.apache.org ).
The PHP program is as follows:
The effect_transform () function takes XML and XSL files as parameters and can be a file name (for example, foo. xml) or a URL (for example, http: // localhost/foo. xml ).
<? Php
Function effect_transform ($ xml, $ xsl ){
// Create a jsontprocessorfactory object. jsontprocessorfactory is a Java
// Class which manufactures the processor for processing Ming transformations.
$ Export tprocessorfactory = new java ("org. apache. xalan. xslt. XSLTProcessorFactory ");
// Use the specified tprocessorfactory method getProcessor () to create
// New jsontprocessor object.
$ Effectprocessor = $ effectprocessorfactory-> getProcessor ();
// Use your tinputsource objects to provide input to the specified tprocessor
// Process () method for transformation. Create objects for both
// Xml source as well as the XSL input source. Parameter
// Using tinputsource is (in this case) a 'system identifier' (URI) which
// Can be an URL or filename. If the system identifier is an URL, it
// Must be fully resolved.
$ XmlID = new java ("org. apache. xalan. xslt. small tinputsource", $ xml );
$ StylesheetID = new java ("org. apache. xalan. xslt. small tinputsource", $ xsl );
// Create a stringWriter object for the output.
$ StringWriter = new java ("java. io. StringWriter ");
// Create a ResultTarget object for the output with the specified tresulttarget
// Class. Parameter of Parameter tresulttarget is (in this case) a 'character
// Stream ', which is the stringWriter object.
$ ResultTarget = new java ("org. apache. xalan. xslt. parse tresulttarget", $ stringWriter );
// Process input with the specified tprocessors 'method process (). This
// Method uses the XSL stylesheet to transform the XML input, placing
// The result in the result target.
$ Effectprocessor-> process ($ xmlID, $ stylesheetID, $ resultTarget );
// Use the stringWriters 'method toString ()
// Return the buffer's current value as a string to get
// Transformed result.
$ Result = $ stringWriter-> toString ();
$ StringWriter-> close ();
Return ($ result );
}
?>
After the function is defined, we can call it. in the following routine, the variable $ xml points to a URL string, as is $ xsl. This example shows the title of five latest phpbuilder.com articles.
<? Php
$ Xml = "http://www.phpbuilder.com/rss_feed.php? Type = articles & limit = 5 ";
$ Xsl = "http://www.soeterbroek.com/code/xml/rss_html.xsl ";
$ Out = effect_transform ($ xml, $ xsl );
Echo $ out;
?>
If you run a program on a local machine, make sure that your function parameter points to the correct file name.
Although this effect can be achieved through other methods, maybe those methods are better, this example gives you a better understanding of PHP calling JAVA classes.
The tutorial is over. I hope you can learn something from this tutorial. Here are some links you can use:
Http://www.php4win.de ~ A great Win32 distribution of PHP
Http://www.javasoft.com ~ Sun's Java release
Http://www.jars.com ~ Start searching for handy Java classes
Http://www.gamelan.com ~ More Java classes
Http://www.technetcast.com/tnc_play_stream.html? Stream_id = 400 ~ Sam Ruby about PHP and Java integration at Open Source Convention 2000 (audio)
Http://xml.apache.org ~ Apache XML Project
Http://www.phpbuilder.com/columns/justin20001025.php3 ~ Transforming XML with XSL using Sablotron
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.