On 08.07.25 12:51, Per Nyfelt wrote:
Hi,

I am trying to figure out a way to use @GrabConfig(systemClassLoader=true) in a ScritpEngine (or GroovyShell, does not matter.

The following script works without any problem using the groovy command (.e.g `groovy sqltaskExample.groovy`):

@GrabConfig(systemClassLoader=true)
@Grab('com.h2database:h2:2.3.232')
import groovy.ant.AntBuilder
...

Calling that from java however does not work:, e.g.

public class ShellWithGrabSupport {
   public static void main(String[] args)throws Exception {
     GroovyScriptEngine gse =new GroovyScriptEngine(".");
     gse.run("sqltaskExample.groovy",new Binding());
   }
}

Results in "No Suitable classloader found for Grab"
... // This mimics what the `groovy` CLI does
GroovyMain.main(new String[]{"sqltaskExample.groovy"});


All with the same result.


If you run the program using the groovy command or a shell then there is a GroovyClassLoader involved can be used instead of the systemloader.

Basically since Java9 Java does not use a url class loader as system loader anymore. Which means we cannot support systemClassLoader=true in case there is no rescuing GroovyClassLoader


I use the following to bootstrap java:

gl="$GROOVY_HOME/lib"
v="4.0.27"
cp="$gl/groovy-$v.jar:$gl/groovy-ant-$v.jar:$gl/groovy-ant-$v.jar:$gl/ant-1.10.15.jar:\
$gl/ant-launcher-1.10.15.jar:$gl/ivy-2.5.3.jar"
java -cp"$cp" ShellWithGrabSupport.java

you could try using -Djava.system.class.loader=... to set a class loader using the fully qualified name. It should be a URLClassloader or child of it and I guess the class needs to be on the classpath as well

The alternative is using a small program to load the classpath and start the program. In Groovy we are using RootLoader for this. Or you use GroovyStarter

bye Jochen



Reply via email to