by Richard A. DeVenezia, Back to Home
javaobj requires SAS version 9. javaobj is experimental in the first release of version 9.
Welcome to the javaobj examples page. There is much to examine, but the really advanced stuff is the Gateway.
Before you can run any of the examples you need to start a SAS session within an environment having a CLASSPATH variable. For Windows I have found that I need the Java Runtime Environment installed.
$ sas.exe -set CLASSPATH ./examples/classes
Class does not exist on CLASSPATH.
java.net.URI.http() does not have signature String-String. juri is thus unitialized and will cause another error.
java.net.URI.getLastName() does not exist.
java.net.URI.toString() does not have signature String-Double.
This example shows how a message dialog can be displayed from Java. This is a useful concept for situations where the course of action in a Java class is dependent on user input. Obtaining and handling the user input directly in Java is more convenient than dropping back to SAS DATA Step to obtain and respond to the user input.
The example is coded in an adapter pattern. showOptionDialog() and showMessageDialog() methods of the javax.swing.JoptionPane class are inaccessible to javaobj since they require int type arguments. The adapter invokes the methods for SAS passing the correct types.
You can debug your class development outside of SAS by using a main() to run the parts you want to test. main() can be run from a terminal or command prompt. The following command assumes the java class is located in the current directory.
$ java -cp . Example7
This example demonstrates how non-trivial tasks require more Java programming than SAS programming. It also shows how looping can be defered by having method that performs a loop iteration [getProperty()] each time it is invoked by an external agent (SAS in this case). The agent is responsible for properly recognizing the end of loop condition.
In programming Java applications, it is often important to know what version of the VM is running. The javaobj interface is still experimental and there is no documentation on how it selects the VM it will use. This example will reveal the properties of the VM that is hosting the SAS javaobj.
Example8 is a wrapper class that surfaces the Enumeration returned by the static method System.getProperties().
By assuming all properties are not blank, the class can return a null to indicate the last property has been delivered. SAS uses the blank string (null) as the condition for not requesting more properties.
Class throws an exception because file named 'foobar.file' does not exist.
Often a SAS program will need to respond to an exception thrown by a Java method. The default behavior is to generate a SAS error. Catching exceptions and making information about the exception available should be part of the design of a wrapper class.
According to a little birdie:
SAS version 9.1 will have new Javaobj methods for dealing with exceptions:
- exceptionCheck ()
- exceptionClear ()
- exceptionDescribe ()
This example is a wrapper for FileInputStream, when the constructor throws an exception it generates a SAS error.
Example 10 is Example 9 modified to catch the Exception and show the exception message in the SAS log. Since newlines in a string are not newlines in the SAS log when 'put', each line must be parsed out and put separately.