Top

Calling Java From Lisp


Programs in j's built-in Lisp can use the following functions to instantiate Java classes and call Java methods:

• jclass

jclass class-name => class-ref

Returns a reference to the specified Java class.

        (jclass "java.lang.String") =>
            #<JAVAOBJECT ... class java.lang.String>
• jconstructor

jconstructor class-name &rest parameter-class-names => constructor-ref

Returns a reference to the specified constructor.

        (jconstructor "java.lang.StringBuffer") =>
            #<JAVAOBJECT ... public java.lang.StringBuffer()>

        (jconstructor "java.lang.StringBuffer" "java.lang.String") =>
            #<JAVAOBJECT ... public java.lang.StringBuffer(java.lang.String)>
Java primitive types may be used as parameter class names:
        (jconstructor "java.lang.StringBuffer" "int") =>
            #<JAVAOBJECT ... public java.lang.StringBuffer(int)>
• jmethod

jmethod class-name method-name &rest parameter-class-names => method-ref

Returns a reference to the specified method.

        (jmethod "java.lang.String" "length") =>
            #<JAVAOBJECT ... java.lang.String.length()>

        (jmethod "java.lang.StringBuffer" "append" "java.lang.String") =>
            #<JAVAOBJECT ... java.lang.StringBuffer.append(java.lang.String)>
The parameter class names may be omitted if there is no ambiguity:
        (jmethod "java.lang.StringBuffer" "setLength") =>
           #<JAVAOBJECT ... public synchronized void java.lang.StringBuffer.setLength(int)>
• jstatic

jstatic method-ref class-ref &rest args => result

Invokes a static method.

If method-ref is a string, it is assumed to name a method. class-ref must be a class name or a class reference returned by jclass.

        (jstatic "currentTimeMillis" "java.lang.System") =>
            1043330903587

If method-ref is a method reference returned by jmethod, the class-ref argument is ignored.

        (jstatic (jmethod "java.lang.System" "currentTimeMillis") nil) =>
            1043330944932
• jnew

jnew constructor &rest args => object-ref

Invokes a constructor.

        (jnew (jconstructor "java.lang.String" "java.lang.String") "this is a test") =>
            #<JAVAOBJECT [java.lang.String] this is a test>
• jcall

jcall method-ref instance-ref &rest args => result

Invokes a method on a Java object.

        (setq x (jnew (jconstructor "java.lang.String" "java.lang.String") "this is a test")) =>
            #<JAVAOBJECT [java.lang.String] this is a test>

        (defconstant length-method (jmethod "java.lang.String" "length")) =>
            LENGTH-METHOD

        (jcall length-method x) =>
            14