This is due to ClassLoader. The Loader class inside the shell (i.e. the classes that you define inside the shell) is different from the ClassLoader class, which launches the shell (the banks you need to run the shell). So the Class.forName("Foo", true, this.class.classLoader) command Class.forName("Foo", true, this.class.classLoader) works because you specify the ClassLoader inside the shell
to try
def shell=new GroovyShell() def f=shell.evaluate("class Foo{Foo(){println this.class.classLoader}};def f=new Foo()") println shell.class.classLoader shell.evaluate("println this.class.classLoader") println "-----------" println Class.forName("Foo", true, f.class.classLoader) println Class.forName("Foo", true, this.class.classLoader)
You will see that the first Class.forName works, not the second. running the script is similar because it will create a script class that does not use the ClassLoader shell
Doing Class.forName will not use the same thing as this in the context of your script.
Not sure if this is clear enough :(
source share