Using the Java Object
About the Java Object
The Java object provides a mechanism that is similar to the Java Native Interface (JNI)
for instantiating Java classes and accessing fields and methods on the resultant objects.
You can create hybrid applications that contain both Java and DATA step code.
CLASSPATH and Java Options
In previous versions of SAS, Java classes were found using the JREOPTIONS system
option.
In SAS, you must set the CLASSPATH environment variable so that the Java object can
find your Java classes. The Java object represents an instance of a Java class that is
534 Chapter 22 Using DATA Step Component Objects
found in the current Java classpath. Any class that you use must appear in the classpath.
If the class is in a .jar file, then the .jar filename must appear in the classpath.
How you set the CLASSPATH environment variable depends on your operating
environment. For most operating systems, you can set the CLASSPATH environment
variable either locally (for use only in your SAS session) or globally. Table 22.1 on page
535 shows methods and examples for different operating environments. For more
information, see the SAS documentation for your operating environment.
Table 22.1 Setting the CLASSPATH Environment Variable in Different Operating
Environments
Operating
Environment Method Example
Windows
Globally Windows System
Environment Variable
in Control Panel
Control Panel ð System ð Advanced ð
Environment Variables (Windows XP Classic
view)
SAS configuration file
set classpath c:HelloWorld.jar
Locally SAS command line
-set classpath c:HelloWorld.jar
UNIX
Globally SAS configuration file
set classpath ~/HelloWorld.jar
Locally
EXPORT command
*
export classpath=~/HelloWorld.jar;
z/OS
Globally TKMSENV data set
set TKJNI_OPT_CLASSPATH=/u/userid/java:
/u/userid/java/test.jar: asis
Locally Not available
VMS
Globally
Command line
**
$ define java$classpath disk:[subdir]
abc.jar, disk:[subdir2]def.jar
detach_template.com
script that is generated
in sas$root:
[misc.base] at
installation
define java$classpath disk:[subdir]
abc.jar, disk:[subdir2]def.jar
Locally Not available
*
The syntax depends on the shell.
**
The command line should be defined before you invoke SAS so that the process that the JVM actually
runs in gets the definition as well.
Using the Java Object 535
Restrictions and Requirements for Using the Java Object
The following restrictions and requirements apply when using the Java object:
The Java object is designed to call Java methods from SAS. The Java object is not
intended to extend the SAS library of functions. Calling PROC FCMP functions is
much more efficient for fast in-process extensions to the DATA step, especially when
large data sets are involved. Using the Java object to perform this type of processing
with large data sets takes significantly more time.
The only Java Run Time Environments (JREs) that are supported by SAS are those
that are explicitly required during the installation of the SAS software.
The only Java options that are supported by SAS are those that are set when SAS is
installed.
Ensure that your Java application runs correctly before using it with the Java object.
The use of a percent character (%) in the first byte of text output by Java to the SAS
log is reserved by SAS. If you need to output a % in the first byte of a Java text line,
it must be escaped with another percent immediately next to it (%%).
Declaring and Instantiating a Java Object
You declare a Java object by using the DECLARE statement. After you declare the new
Java object, use the _NEW_ operator to instantiate the object, using the Java object
name as an argument tag.
declare javaobj j;
j = _new_ javaobj("somejavaclass");
In this example, the DECLARE statement tells the compiler that the object reference J is
of type Java. That is, the instance of the Java object is stored in the variable J. At this
point, you have declared only the object reference J. It has the potential to hold a
component object of type Java. You should declare the Java object only once. The
_NEW_ operator creates an instance of the Java object and assigns it to the object
reference J. The Java class name, SOMEJAVACLASS, is passed as a constructor
argument, which is the first-and-only argument that is required for the Java object
constructor. All other arguments are constructor arguments to the Java class itself.
As an alternative to the two-step process of using the DECLARE statement and the
_NEW_ operator to declare and instantiate a Java object, you can declare and instantiate
a Java object in one step by using the DECLARE statement as a constructor method. The
syntax is as follows:
DECLARE JAVAOBJobject-name(“java-class”, <argument-1, … argument–n>);
For more information, see the “DECLARE Statement, Java Object” in SAS Component
Objects: Reference and the “_NEW_ Operator, Java Object” in SAS Component Objects:
Reference.
Accessing Object Fields
Once you instantiate a Java object, you can access and modify its public and class fields
in a DATA step through method calls on the Java object. Public fields are non-static and
declared as public in the Java class. Class fields are static and accessed from Java
classes.
536 Chapter 22 Using DATA Step Component Objects
Method calls to access object fields have one of these forms, depending on whether you
are accessing non-static or static fields:
GETtypeFIELD(“field-name”, value);
GETSTATICtypeFIELD(“field-name”, value);
Method calls to modify object fields have one of these forms, depending on whether you
access static or non-static fields:
SETtypeFIELD(“field-name”, value);
SETSTATICtypeFIELD(“field-name”, value);
Note: The type argument represents a Java data type. For more information about how
Java data types relate to SAS data types, see “Type Issues” on page 537. The field-
name argument specifies the type for the Java field, and value specifies the value that
is returned or set by the method.
For more information and examples, see “Dictionary of Java Object Language
Elements” in SAS Component Objects: Reference.
Accessing Object Methods
Once you instantiate a Java object, you can access its public and class methods in a
DATA step through method calls on the Java object. Public methods are non-static and
declared as public in the Java class. Class methods are static and accessed from Java
classes.
Method calls to access Java methods have one of these forms, depending on whether you
are accessing non-static or static methods:
object.CALLtypeMETHOD (“method-name”, <method-argument-1 …, method-
argument-n>,
<return value>);
object.CALLSTATICtypeMETHOD (“method-name”,
<method-argument-1 …, method-argument-n>, <return value>);
Note: The type argument represents a Java data type. For more information about how
Java data types relate to SAS data types, see “Type Issues” on page 537.
For more information and examples, see “Dictionary of Java Object Language
Elements” in SAS Component Objects: Reference.
Type Issues
The Java type set is a superset of the SAS data types. Java has data types such as BYTE,
SHORT, and CHAR in addition to the standard numeric and character values. SAS has
only two data types: numeric and character.
The following table describes how Java data types are mapped to SAS data types when
using the Java object method calls.
Table 22.2 How Java Data Types Map to SAS Data Types
Java Data Type SAS Data Type
BOOLEAN numeric
BYTE numeric
Using the Java Object 537
Java Data Type SAS Data Type
CHAR numeric
DOUBLE numeric
FLOAT numeric
INT numeric
LONG numeric
SHORT numeric
STRING
character
*
*
Java string data types are mapped to SAS character data types as UTF-8 strings.
Other than STRING, it is not possible to return objects from Java classes to the DATA
step. However, it is possible to pass objects to Java methods. For more information, see
“Passing Java Object Arguments” on page 540.
Some Java methods that return objects can be handled by creating wrapper classes to
convert the object values. In the following example, the Java hash table returns object
values. However, you can still use the hash table from the DATA step by creating simple
Java wrapper classes to handle the type conversions. Then you can access the dhash
and shash classes from the DATA step.
/* Java code */
import java.util.*;
public class dhash
{
private Hashtable table;
public dhash()
{
table = new Hashtable ();
}
public void put(double key, double value)
{
table.put(new Double(key), new Double(value));
}
public double get(double key)
{
Double ret = table.get(new Double(key));
return ret.doubleValue();
}
}
import java.util.*;
public class shash
{
538 Chapter 22 Using DATA Step Component Objects
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.221.144