Skip to main content
    Country/region select      Terms of use
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

I've been needing to use LS2J recently so that I could code DNS lookups and web page requests as part of a testing system I'm working on for LotusLive. The documentation is a little vague on how you work with methods in a Java class from LotusScript, so since I had to experiment to find out how it worked, I figured it would be nice to record the information here.

In most cases, you're writing the Java classes you're calling via LS2J, so you get to decide whether there are static members. But maybe you got the class from elsewhere and you don't want to have to modify it, or maybe you're just using a built-in class that you don't get to change. So it may occasionally be helpful to know how to use the static members. So here goes.

You can invoke a static method of a Java class using the JavaMethod.Invoke method with an object argument of Nothing. First you need to get a handle to the method. So for instance, to invoke the static method HTTPPing, which takes a String argument and returns an int, I would write:

       Dim jses As New JavaSession
Dim jclass As Javaclass
Set jclass = jses.GetClass("TCPClient")
Dim result%, url$
      url =
      result = jclass.Getmethod(
"HTTPPing", "(Ljava/lang/String;)I").Invoke(Nothing, url)

The second argument to Getmethod, the method signature, can be a bit hard to come up with. The example in the help shows a sample value of "(I)Ljava/lang/String;", but a single example isn't much help unless the method you want has exactly that signature (and the example seems to be wrong anyway). Rather than try to puzzle out what the right signature might be for the method, I wrote an agent to scan the methods of a JavaClass object and display all the signatures.

       Dim jses As New JavaSession

        Dim jclass As Javaclass
Set jclass = jses.GetClass("TCPClient")
Dim jmc As Javamethodcollection
Dim jm As Javamethod
Set jmc = jclass.Getclassmethods
Set jm = jmc.Getfirst
Dim methods$
Do Until jm Is Nothing
               methods = methods &
& jm.Methodname & " - '" & jm.Signature & "'"
Set jm = jmc.Getnext
MessageBox Mid$(methods, 2)

which gives the following output:

equals - '(Ljava/lang/Object;)Z' 
hashCode - '()I'

toString - '()Ljava/lang/String;'

getClass - '()Ljava/lang/Class;'

notify - '()V'

notifyAll - '()V'

wait - '()V'

wait - '(J)V'

wait - '(JI)V'

ping - '(Ljava/lang/String;II)Z'

DNSLookup - '(Ljava/lang/String;)Ljava/lang/String;'

HTTPPing - '(Ljava/lang/String;)I'

So we can see from the last entry exactly what the signature argument should be.

Andre Guirard | 26 May 2011 12:49:02 PM ET | Home, Plymouth, MN, USA | Comments (0)


No Comments Found

 Add a Comment
Comment:  (No HTML - Links will be converted if prefixed http://)
Remember Me?     Cancel

Search this blog 


    About IBM Privacy Contact