How do you use a custom class in a console script?

Will November 11, 2015

I tested using a custom class following the sample script and class included in the ScriptRunner documentation, https://scriptrunner.adaptavist.com/latest/jira/#_script_roots, and it worked correctly in the Script Console specifying the script file. On the server the script file (foo.groovy) is placed in this directory, <jira-home>\scripts. The class file (Bollo.groovy) was placed in <jira-home>\scripts\util. I even tested changing the class directory (e.g. custom). This required changing the corresponding import call in the script (e.g. change 'import util.Bollo' to 'import custom.Bollo') and changing the package declaration in the class file (e.g. change 'package util' to 'package custom'). When I tested the script in the Script Console specifying the script file it worked. However, when I tried to paste the script from the file into the Script Console I get 'unable to resolve class' error when I click run. It appears that ScriptRunner console script is run so that the relative path location does not work like it does for the script file. I am trying to find some information on how to address the script root location in the ScriptRunner console script or where the class needs to be placed to be accessible to the ScriptRunner console script. It appears that the JIRA classes are stored in C:\Program Files\Atlassian\JIRA\atlassian-jira\WEB-INF\classes\com. However, adding a sub-directory to this location with the custom class does not make the class accessible. Can anyone help me to get my class working with a console script?  I am sure there is a simple solution but I cannot find it.  Thank you for any help.

System Info:

JIRA - v6.3.15

ScriptRunner - 3.1.3

 

Files:

foo.groovy

import util.Bollo

log.debug ("Hello from the script")

Bollo.sayHello()

 

Bollo.groovy

package util

public class Bollo {

                public static String sayHello() {

                                "hello sailor!!!"

                }

}

2 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 11, 2015

Hi Will,

I am afraid is not possible to import a class that exists in a file, in a script that exists in your Script runner console. I think this link https://jamieechlin.atlassian.net/browse/GRV-544 (especially the comments) will help you understand more.

Regards

Thanos 

1 vote
Jeff Louwerse
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 11, 2015

What I did was create a scratch file (I called it scratch.groovy) and I simply use that for testing all my code instead of the console... and then run it as a file.   I created a simple PHP interface to edit the code, upload new code and run it via a REST call.   using this method I didn't have to worry about where I could us basescript vs. my own class.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events