Re: using @GrabConfig(systemClassLoader=true) in a ScriptEngine
Jochen Theodorou <[email protected]> Wed, 9 Jul 2025 17:46:19 +0200
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <[email protected]> |
On 08.07.25 12:51, Per Nyfelt wrote:
> Hi,
>=20
> I am trying to figure out a way to=20
> use=C2=A0@GrabConfig(systemClassLoader=3Dtrue) in a ScritpEngine=C2=A0(o=
r=20
> GroovyShell, does not matter.
>=20
> The=C2=A0following script works without any problem using the groovy com=
mand=20
> (.e.g `groovy sqltaskExample.groovy`):
>=20
> @GrabConfig(systemClassLoader=3Dtrue)
> @Grab('com.h2database:h2:2.3.232')
> import groovy.ant.AntBuilder
> ...
>=20
> Calling that from java however does not work:, e.g.
>=20
> public class ShellWithGrabSupport {
> public static void main(String[] args)throws Exception {
> GroovyScriptEngine gse =3Dnew GroovyScriptEngine(".");
> gse.run("sqltaskExample.groovy",new Binding());
> }
> }
>=20
> Results in "No Suitable classloader found for Grab"
>...=20
> // This mimics what the `groovy` CLI does
> GroovyMain.main(new String[]{"sqltaskExample.groovy"});
>=20
>=20
> All with the same result.
If you run the program using the groovy command or a shell then there is=
=20
a GroovyClassLoader involved can be used instead of the systemloader.
Basically since Java9 Java does not use a url class loader as system=20
loader anymore. Which means we cannot support systemClassLoader=3Dtrue in=
=20
case there is no rescuing GroovyClassLoader
> I use the following to bootstrap java:
>=20
> gl=3D"$GROOVY_HOME/lib"
> v=3D"4.0.27"
> cp=3D"$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=3D... to set a class loader=
=20
using the fully qualified name. It should be a URLClassloader or child=20
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=
=20
the program. In Groovy we are using RootLoader for this. Or you use=20
GroovyStarter
bye Jochen