Herr Derb Herr Derb - 1 year ago 88
Groovy Question

Script working in online editor but not in java GroovShell (No such property)

I'm working for the first time with

. I have a simple script, that I want to run on my Java server.

I build the script here MyScript in the Groovy web console, and when I run it, it returns what I expect


Object test(int arg){
return getRange();

Object[] getRange(){
if (rangeType=="NONE"){
return [];

if (rangeType=="RANGE"){
return [minValue,maxValue];

if (rangeType=="LIST"){
return listValues;

if (rangeType=="ENUM"){
return enumMap;


On my Java server I invoke the
method this way

Script groovyScript = new GroovyShell().parse(script);
return groovyScript.invokeMethod("test", valueSuccess);

Although the script runs fine in the web console, when I run it on my server it gives me the following exception:

groovy.lang.MissingPropertyException: No such property: rangeType for class: Script1

The exact same script once runs without a problem, once it throws an exception. How can that be? It's not even complex, and the variable declarations should be correct, aren't they?

Answer Source

I would like you to import the field annotation package and correct the decalartion of variables.Specify some datatype for them along with the @Field annotation to access the variable anywhere in the script.

import groovy.transform.Field

@Field int minValue   = 1;
@Field int maxValue   = 10;
@Field List listValues= [];
@Field Map enumMap    = [:];
@Field def rangeType  = "RANGE"; //RANGE,LIST,ENUM,NONE

reference link for creating and accessing the global variables in Groovy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download