How To

How do I invoke a jar file from a Groovy script in IDM/OpenIDM (All versions)?

Last updated Jul 30, 2018

The purpose of this article is to provide information on class loading from a Groovy script in IDM/OpenIDM.


Overview

The GroovyScriptLoader is used to load classes within a Groovy script. You can use the information in this article to specify the classpath for classes loaded directly as well as those loaded indirectly.

If you experience issues with classes not being found, for example, you see errors such as NoClassDefFoundError or ClassNotFoundException:

SEVERE: Script invocation error 
java.lang.NoClassDefFoundError: full/class/name

...
Caused by: java.lang.ClassNotFoundException: full.class.name
        at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
        at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:648)

You can resolve it by specifying the classpath for the implementing jar files.

Finding classes

You can find classes in an IDM/OpenIDM installation as follows:

  1. Run the following command from the root directory of the installation:
    for x in */*.jar; do echo ">>> $x"; jar tvf $x; echo "<<< $x"; echo; done > jars.log
    
    The resulting file will list all classes found in any jar files.
  2. Search the list of classes for the one referenced in the error.
  3. Scroll up or down from the class to locate the jar file that contains it.
  4. Invoke the required jar file per the steps in this article.

Invoking a jar file from a Groovy script (IDM 6 and later)

You can invoke a jar file from a Groovy script as follows:

  1. Add the external jar file to the /path/to/idm/lib directory.
  2. Update the script.json file (located in the /path/to/idm/conf directory) to append the complete path to the jar file in the groovy.classpath, per your operating system:
    • Unix® and Linux® systems:
      "groovy.classpath" : "&{idm.install.dir}/lib:&{idm.install.dir}/lib/custom-lib-test-1.0.0.jar",
      
    • Microsoft® Windows® systems: (The groovy.classpath requires a semicolon ';' separator on Windows instead of a ':')
      "groovy.classpath" : "&{idm.install.dir}/lib;&{idm.install.dir}/lib/custom-lib-test-1.0.0.jar",
      
  3. Update the Groovy script to import the class(es) and invoke them, for example:
    import org.sample.Sample
    
    samp = new Sample(); java.lang.System.out.println('It is working: ' + samp.reverse("testing"));
    
Note

You can add multiple jars to the class path as detailed in Integrator's Guide › Setting the Script Configuration.

Invoking a jar file from a Groovy script (pre-IDM 6)

You can invoke a jar file from a Groovy script as follows:

  1. Add the external jar file to the /path/to/idm/lib directory.
  2. Update the script.json file (located in the /path/to/idm/conf directory) to append the complete path to the jar file in the groovy.classpath, per your operating system:
    • Unix® and Linux® systems:
      "groovy.classpath" : "&{launcher.install.location}/lib:&{launcher.install.location}/lib/custom-lib-test-1.0.0.jar",
      
    • Microsoft® Windows® systems: (The groovy.classpath requires a semicolon ';' separator on Windows instead of a ':')
      "groovy.classpath" : "&{launcher.install.location}/lib;&{launcher.install.location}/lib/custom-lib-test-1.0.0.jar",
      
  3. Update the Groovy script to import the class(es) and invoke them, for example:
    import org.sample.Sample
    
    samp = new Sample(); java.lang.System.out.println('It is working: ' + samp.reverse("testing"));
    
Note

You can add multiple jars to the class path as detailed in Integrator's Guide › Setting the Script Configuration.

Troubleshooting

If the jar file is not loaded, you should try the following process to remove any outdated generated classes:

  1. Shutdown the IDM/OpenIDM instance.
  2. Delete the install-dir/classes directory.
  3. Delete the contents of the /path/to/idm/felix-cache directory.
  4. Start the IDM/OpenIDM server.

See Also

How do I call one Groovy script from another in IDM/OpenIDM (All versions)?

How do I add logging to Groovy scripts in IDM/OpenIDM (All versions)?

Scripts in IDM/OpenIDM

Integrator's Guide › Setting the Script Configuration

Related Training

N/A

Related Issue Tracker IDs

N/A



Copyright and TrademarksCopyright © 2018 ForgeRock, all rights reserved.
Loading...