Skip to main content
Skip table of contents

Scripting with Java

Scripting can support invocation of Java code. Java may be invoked in a script or as an action for an event.

Java code can be invoked from Scheme in several ways:

  • Create an instance of a built-in object that implements nexj.core.scripting. This is the case for most Java presentation layer code. This method is usually only used by internal NexJ developers.
  • Use other built-in libraries that don't implement nexj.core.scripting. In this case, the Java class is examined using Java reflection and all public methods and fields are available. As well, the following JavaBean-style transformations are performed on Java method names.
    • getPropertyX() is accessible in script as (instance'propertyX).
    • setPropertyX(Object value) is accessible in script as (instance'propertyX value).
    • isPropertyX() is accessible in script as (instance'propertyX).
    • isTLAPropertyX() is accessible in script as (instance'tlaPropertyX).
    • getXWithArguments(Object, Object) is accessible in script as (instance'getXWithArguments a b) with no transformation.
    • setXWithArguments(Object value, Object value2) is accessible in script as (instance'setXWithArguments value value2) with no transformation.

    To see a list of built-in Java libraries, enter nexj or java in a script editor and press Ctrl+Space.

  • Import a class and then construct an instance or invoke it statically. To do this, first use the import function to make it accessible to the Scripting VM. Once imported, refer to the variable of the same name as the fully qualified Java class name to affect static fields and methods. To invoke the constructor, use the 'new member. This method should never be used in client-side code, as it will not work on most clients.

    For example, (import <fullyQualifiedJavaClass>)

    To import an inner class, use the dollar sign ($) as the last separator. For example, (import 'nexj.core.persistence.sql.SQLSchemaManager$SQLConnectionAppender)

Example of using pre-referenced built-in Java classes

  1. Start the Scheme Console.
  2. In the  Resources tab, create a new scratchpad called "Java".
  3. In your scratchpad, type nexj and press Ctrl+Space. You should see a list of built-in NexJ Java libraries.
  4. Try the following static methods and observe the results.

    SCHEME
    > (nexj.core.util.GUIDUtil'NULL_GUID)
    ; #z00000000000000000000000000000000
    
    > (nexj.core.util.GUIDUtil'generateGUID)
    ; zB34BDA610D25444A89537A0AEA9B4981
    
    > (nexj.core.util.Base64Util'encode (bytevector 0 -127 255 -1 -2))
    ; "AIH///4="
  5. Try the following instance code and observe the results:

    SCHEME
    > (define dte (java.util.Date'new "01/25/2011"))
    ; #<Tue Jan 25 00:00:00 EST 2011>
    
    > (dte'toGMTString) ; notice that <CTRL><Space> knows what dte is
    ; "25 Jan 2011 05:00:00 GMT"

Example of importing unreferenced built-in Java classes

Try the following code in your scratchpad and Scheme console.

SCHEME
> (import 'java.util.Random)
; 14:35:44,331 DEBUG [GlobalEnvironment] (NexJ-ContainedProcess) Importing class
 java.util.Random

> (define rnd (java.util.Random'new))
; #<java.util.Random@59ab51b0>

> (rnd'nextInt)
; -2065740977

Importing Java classes in JARs

The above example works for classes that are part of Java or the NexJ framework.

If you want to use other libraries, such as your own custom encryption or integration classes, then you need to add them to your model first.

To do this for externally built JARs:

  1. In NexJ Studio, navigate to the Java perspective.
  2. Create a folder called lib at the top level, that is, the same level as the meta folder.
  3. Place the JAR that contains your classes into the lib folder.
  4. Restart your console.

You can now reference classes from your JAR using the import function.

Example of building Java code in your model

To include and reference Java code in your model:

  1. If your project isn't already a Java Project, open the .project file and add the Java nature and buildCommand sections. For example:

    XML
    <projectDescription>
       <name>core</name>
       <comment></comment>
       <projects>
       </projects>
       <buildSpec>
     <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand>       <buildCommand>
             <name>com.nexjsystems.nexjstudio.NexJBuilder</name>
             <arguments>
             </arguments>
          </buildCommand>
       </buildSpec>
     <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures> 
    </projectDescription>
  2. Create a new source folder in your project called src. To create a new project, from the File menu, select New > Source Folder.
  3. Create a new class. Name the package scripting.demo and name the type Hello.
  4. Add a getHello method to your class. For example:

    JAVA
    package scripting.demo;
    
    public class Hello {
    
       private String m_hello = "Hello";
    
       public String speak() {
          return m_hello;
       }
    }
  5. Save your file and from the Project menu, select Clean. In the dialog that appears, select your project and click Clean.
  6. Confirm that the built file is in the bin folder.
  7. Run the Scheme console and enter the following:

    SCHEME
    > (import 'scripting.demo.Hello)
    ; 15:56:14,924 DEBUG [GlobalEnvironment] (NexJ-ContainedProcess) Importing class scripting.demo.Hello
    
    > (define demo (scripting.demo.Hello'new))
    ; #<scripting.demo.Hello@1a03ff34>
    
    (demo'speak)
    ; "Hello"
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.