TechniqueCreate your class, place it in your Java lib file, and then call the class file with the syntax described in recipe 13.6. The Java class: SimpleClass public class SimpleClass { public string get_greeting(string name) { return "Hello, there, " + name + "."; } } The PHP script to access SimpleClass : <?php $jclass = new Java("SimpleClass"); print $jclass->get_greeting ("Joe"); ?> CommentsWe created our own simple Java class ( SimpleClass ) with a method ( get_greeting ), and then called that class from within our PHP program. That is basically how you do it, but remember that your class must be in a special directory where all the predefined classes for the JVM are stored (for me, it is windows \java\trustlib ). Consult your Java documentation for more information. This is a simple example; nothing is accomplished that we couldn't have accomplished in PHP. So, how is this useful? Assume that we have a mainframe that we need Java to connect to, and we need to connect that mainframe to the Internet. We might be able to use something like a Java applet or Java servlets, but a much simpler solution is to use PHP to connect to Java, which in turn would connect to the mainframe. We are done! This is such an important feature that it certainly deserves a more powerful example. Therefore, I wrote the following Java class, Zipper , which zips files into *.zip files that can then be distributed from your Web site. Interfacing with this class enables us to create zip files on-the-fly and give them to our users. (I am not doing tar.gz files because PHP already offers this functionality. See http://www.php.net/manual/html/_ref.zlib.html for more information.) First, the Java source file: Zipper.java import java.io.*; import java.util.zip.*; public class Zipper { public int chunk = 8192; public string Zipem ( String files, String zipped ) { String this_file = ''; if (files.length() < 1) { return "You need to have some files to Zip"; } // Output stream try { FileInputStream foward = new FileInputStream(zipped); ZipOutputStream final = new ZipOutputStream(foward); } catch (IOException e) { return "Cannot create " + zipped + " an error occurred"; } do { if (files.indexOf("" != -1) { this_file = files.substring(0, files.indexOf("") ); try { ZipEntry ent; if (this_file.indexOf("/") != -1) { ent = new ZipEntry(this_file.substring( this_file.lastIndexOf("/") +1, this_file.length()) ); } else { ent = new ZipEntry(this_file.substring( this_file.lastIndexOf("\") +1, this_file.length()) ); } final.putNextEntry(ent); } catch (IOException e) { return "Cannot prepare " + zipped + "Unable to add" + this_file; } files.substring( files.indexOf("")+1 ); byte[] buf = new byte[chunk]; //Compress that file try { FileInputStream reader = new FileInputStream(this_file); int length; while ((length = reader.read(buf, 0 chunk)) != -1) { final.write(buf, 0, length); } reader.close(); } catch (IOException e) { return "Can't compress " + this_file; } } } while (files.indexOf("") != -1); try { ZipEntry ent; if (files.indexOf("/") != -1) { ent = new ZipEntry(files.substring( files.lastIndexOf("/") + 1, files.length()) ); } else { ent = new ZipEntry(files.substring( files.lastIndexOf("\") + 1, files.length()) ); } final.putNextEntry(ent); } catch (IOException e) { return "Cannot prepare " + zipped + " unable to add " + this_file; } byte[] buf = new byte[chunk]; try { FileInputStream reader = new FileInputStream(files); int length; while ((length = reader.read(buf,0,chunk)) != -1) { final.write(buf,0,length); } reader.close(); } catch (IOException e) { return "Cannot compress " + files; } try { final.close(); } catch (IOException e) { return "Cannot create" + zipped; } return zipped + " has been created"; } } Now the PHP script that interfaces with this class: <?php $zipObj = new Java("Zipper"); $files = array("/home/designmm/designmultimedia-logs/access_log", "/home/designmm/designmultimedia-logs/error_log"); $ret_val = $zipObj->ZipEm(implode("", $files), "logs.zip"); if ($ret_val == "logs.zip has been created") { header("http://www.designmultimedia.com/logs/logs.zip"); } else { ?> <html> <head> <title> Sorry there was an error <?php echo $ret_val; ?></title> </head> <body> <h1> I'm Sorry there seems to have been an error </h1> <br> <b> <?php echo $ret_val; ?> </b> <br><br> Please try again some other time. </body> </html> <?php } ?> |