HomeOracle Page 4 - Extending PL/SQL with Java Libraries
Building Java Class Libraries in Oracle - Oracle
If you are looking for a way to extend stored programs with Java, look no further. This article, the first of two parts, is excerpted from chapter five of Expert Oracle PL/SQL, written by Ron Hardman and Michael McLaughlin (McGraw-Hill/Osborne, 2005; ISBN: 0072261943).
When you choose to build Java class libraries, you have two deployment choices. You can build call-interface driven (middle-tier) or server-side Java class libraries.
Call-interface libraries act like server-side includes to your Apache server. They must be replicated to all nodes of your Apache server and are managed within the structure of your web server load-balancing tool. These components act like external programs that call into the Oracle server and are better treated in Enterprise Java books.
Why Would I Use This?
You need to know when a Java class belongs as an internal or external library. We find Java libraries deployed internally in the database have well-defined but narrow uses. Likewise, we find external libraries to be powerful components but less convenient than locally stored objects. You will need to understand these two technologies to make good deployment choices.
NOTE
While call-interface driver or middle-tier Java class libraries are not directly covered, they do require direct reference in their path to the Oracle OCI libraries. The OCI libraries are in the Oracle Application Server but not on other web servers.
Server-side Java class libraries are stored objects within the Oracle JVM, which is a subcomponent of the Oracle database. Server-side Java class libraries are the core theme of this chapter. In the next two sections, you値l learn how to build internal server Java functions and procedures.
NOTE
If you池e unfamiliar with configuring and testing a Java JDBC connection, please check Appendix D for instructions.
Java programming ranges from simple to complex, but these examples should be straightforward. You have two core executables to run Java programs, which you値l use in the examples. They are
javac Compiles your text file Java programs into Java byte code
java Runs your compiled Java byte code programs
The file-naming convention in Java is case-sensitive so you should ensure you name files consistent with the web-based code example files. If you attempt to compile a Java file when the file name and class name are different, you値l receive an error. Also, the file extension for Java programs is a lowercase .java.
You値l now build a simple HelloWorld1.java file to make sure the balance of the examples works. If you池e working in Microsoft Windows, please open a Command Prompt Window. If you池e working in UNIX, please use a terminal window. The following is the code for HelloWorld.java:
-- Available online as part of HelloWorld1.java file. // Class definition. public class HelloWorld1 { // --------------------------------------/ // Static main to print Hello World. public static void main(String args[]) { // Print the message to console. System.out.println("Hello World."); } // End of static main. // --------------------------------------/ } // End of HelloWorld1 class.
Java text files are compiled by the following syntax:
javac HelloWorld1.java
Successful compilation does not return anything to the console. The lack of any message is a good thing. The way to verify whether or not you have a Java byte code file is to run the Microsoft Windows directory (dir) command or UNIX list (ls) command for files matching HelloWorld1.* in the present working directory. You should see two files displayed to the console:
HelloWorld1.java HelloWorld1.class
After building the Java byte code compiled program, you can test its execution by doing the following:
java HelloWorld1
NOTE
You do not provide the .class extension when running Java programs because it is assumed. Appending .class to the file name will raise the followingexception: java.lang.NoClassDefFoundError: HelloWorld1/class.
TIP
You can also raise the java.lang .NoClassDefFoundError: HelloWorld1/class error if you do not have your present working directory in your PATH and CLASSPATH environment variables.
You値l receive the following results:
Hello World.
The next section covers how you build server-side or internal server Java programming units. You値l learn how to build Java class files to support stored functions and procedures and how to wrap their existence in PL/SQL packages. The following two sections are sequential and the second section assumes you have worked through the first.