You seem to be wanting to access script bindings but are supplying an explicit class that isn't a script (doesn't extend Script). I'd just replace your script definition with this:
final String SCRIPT = "println \"Hello ${foo}\""; Cheers, Paul. <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.www.avast.com <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> On Mon, Nov 13, 2023 at 4:50 AM Jochen Wiedmann <jochen.wiedm...@gmail.com> wrote: > > Hi, > > could anyone, please, explain to me what to fix in the script below? I > am getting the error message below: > > Thanks, > > Jochen > > > <<<Error>>> > : Apparent variable 'foo' was found in a static scope but doesn't > refer to a local > variable, static field or class. Possible causes: > You attempted to reference a variable in the binding or an instance > variable > from a static context. > You misspelled a classname or statically imported field. Please check the > spelling. > You attempted to use a method 'foo' but left out brackets in a place not > allowed by the grammar. > @ line 7, column 22. > demo.foo = "${foo}"; > <<</Error>>> > > My Java class looks like this: > > <<<Java code>>> > public static void main(String[] args) { > final String SCRIPT = > "public class Demo {\n" > + " public void run() {\n" > + " System.out.println(\"Hello ${this.foo}\");\n" > + " }\n" > + " public static void main(String[] args) {\n" > + " Demo demo = new Demo().run();\n" > + " demo.foo = \"${foo}\";\n" > + " }\n" > + "}\n"; > final GroovyShell gsh = new GroovyShell(); > final Map<String,Object> parameters = new HashMap<>(); > parameters.put("foo", "World"); > final Script script = gsh.parse(SCRIPT, new Binding(parameters)); > script.run(); > } > <<</Java code>>> > > > > > -- > The woman was born in a full-blown thunderstorm. She probably told it > to be quiet. It probably did. (Robert Jordan, Winter's heart)