Previous Topic: Configure the Run Java Code OperatorNext Topic: Custom Operators


Using a JavaObject

Java objects are saved after a Run Java Code operator has completed in a JavaObject data type. You can use a JavaObject dataset variable in the following ways:

Observe the following constraints when working in JavaScript:

The Java code that you write can consist of normal Java statements and expressions. You can also define your own methods and use them inside the code. For example:

// Import the classes that you want to use
import ca.tech.pam.MyAccount;
// Note: no need to import StringBuffer and Date because they are part of the 
// automatically imported packages
// import java.lang.StringBuffer;
// import java.util.Date;
// Note: the jar that contains the ca.tech.pam.MyAccount class 
// must be in the list of External Jars of the operator or the module;
// but java lang and java util are in rt.jar, which is automatically put in the classpath

MyAccount acct = new MyAccount(1000.00);

// Use the public methods of the MyAccount object
acct.addFunds(34.44);
acct.subFunds(10);

// Define your own method
String getStatement(MyAccount acc) {
    StringBuffer strBuff = new StringBuffer("Account Balance: " + acc.getBalance());
    Date dt = new Date(System.currentTimeMillis());
    strBuff.append(" on date: " + dt);
    return strBuff.toString();
}
// Use the method you defined
// also print the statement using the 'logger' object that you 
// setup in the 'Logger' page of the operator 
logger.info(getStatement(acct));

After you run this Java code, the log message shows the account balance, the date, and the time:

Account Balance: 124.44 on date: Wed Jul 13 12:53:37 EDT 2011