Including a Java Class in SSJS

You’re most likely aware that SSJS really is really turned into Java under the covers, but did you realize that you can use Java classes in SSJS? In this post, I’ll show how with a simple example.

Using a Java class in SSJS

There are examples similar to this in the DDE help when looking up how to get a document from a view:

var vec = new java.util.Vector();
vec.addElement('firstLookupKey');
vec.addElement('secondLookupKey');
var doc - myView.getDocumentByKey(vec);

And so on. Several view lookup methods take a vector as a way to look up documents or view entries with multiple keys for multiple sorted columns.

The first line specifies that it’s creating a new java.util.Vector() object. After that, you can use methods of that Java class right from SSJS.

Using Type Ahead Autocompletion for Java Class Methods

If you specify the type of the variable that you create, Domino Designer provide type ahead. Take this example:

var vec:java.util.Vector = new java.util.Vector();

Just type the variable name and a period, and (maybe after a slight pause), you’ll see a list of properties and methods to choose from:

Java Class in SSJS

Importing a Java Class into SSJS

If you want to refer to an object more easily in SSJS, you can import the class you want to use and then reference it just by the class name, rather than by the fully-qualified name. To do this, use importPackage().

Here’s how this would look different when working with a Vector:

importPackage (java.util.Vector);	
var vec = new Vector();

And now you have code that’s a little easier to follow and also has the ability to display the properties and methods of the class:

Java Class in SSJS 2

Clearly, there is far more opportunity to use this than the simple examples I’ve shown here, but I hope this gets the gears turning and becomes a useful feature for you.

Advertisements

Tags:

5 responses to “Including a Java Class in SSJS”

  1. davidleedy says :

    Just to add a little bit here for anyone interested. I’ve talked about this a couple times on different NotesIn9’s so if you want other examples I believe these 2 shows take advantage of Java Objects inside SSJS:
    http://www.notesin9.com/2011/07/11/notesin9-ee-009-using-java-hashmaps-and-treemaps-with-xpages/
    http://www.notesin9.com/2011/04/01/notesin9-025-selecting-documents-from-a-repeat-control/

  2. Howard says :

    Brad, suppose we created our own Java class and import that into SSJS. Is there a way to get typeahead working? Do we need to create a JavaDoc or something?

    thanks,

    Howard

    • davidleedy says :

      I’ve no idea how to get typeahead working in SSJS for a custom java object. I asked around a while back and I don’t think it’s going to be practical. However the way to kinda mitigate that is by moving a lot of what you would do in SSJS into the “controller classes” that Jesse came up with. LOVE THAT!!!! Coming very soon to a NotesIn9 near you! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: