[groovy-user] GroovyScriptEngine classpath issues

403 views
Skip to first unread message

jean-philippe robichaud

unread,
Jul 6, 2011, 12:32:45 PM7/6/11
to us...@groovy.codehaus.org
Hello everyone,

I'm fighting with the GroovyScriptEngine (or GroovyShell) to achieve the following:
- I want to be able to quickly prototype various things and write script without having to worry about setting the classpath correctly
- I want to avoid using ~/.groovy because of the nature of the environment we're using
- I would like to avoid having to compile the groovy classes/script as much as possible
- ideally, this wrapper script has to work with groovy 1.7.5 and 1.8.0

The idea is to be able to do something like this:
groovy wrapper.groovy path/to/my_groovy_script.groovy arg1 arg2 arg3...

In the directory where wrapper.groovy is, there is a lib directory containing various jar files.  Also, in the directory where my_groovy_script.groovy script resides, there is potentially some other groovy scripts/classes in the form a source files (with package name).

Now the following wrapper.groovy code sorta works when I'm using my gentoo environment (where groovy 1.7.5 is compiled from sources using the magical ebuild from gentoo) but it doesn't work if I'm trying to use a binary distribution of groovy (1.7.x or 1.8).

What am I doing wrong?

I can provide some script samples if that would help, but I guess I'm just doing something obviously wrong...

Thanks for your help!

Jp

wrapper.groovy:
import groovy.lang.Binding;
import java.lang.reflect.*;
import groovy.util.GroovyScriptEngine;

def myargs = [];
debug = true;

args.eachWithIndex{a,i ->
    if(i != 0){
        myargs << a;
    }
}


def cp = [];
def f = new File(args[0]);
cp << f.getCanonicalFile().getParent();
cp << f.getAbsoluteFile().getParentFile().getParent();
def jarFiles = loadJars("lib", cp);

def local_uri  = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
def local_file = new File(local_uri);
// this is the lib directory that exists in the same directory as the one where wrapper.groovy (this script) is
def local_lib = new File(local_file.getParentFile(), "lib").getPath();

String[] roots = cp;
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
URL[] rootsURLs = cp.collect { "file://" + it}*.toURL();

URLClassLoader child = new URLClassLoader (rootsURLs , this.getClass().getClassLoader());

loadJarsToClassLoader(local_lib, child);
Thread.currentThread().setContextClassLoader(child);
if(debug) {
    println "child class loader :";;
    child.getURLs().each { p -> println "   => $p"; }
}


GroovyScriptEngine gse = new GroovyScriptEngine(roots, child);

def cfg = gse.getConfig();
def lst = child.getURLs().collect{ it.toString()};
cfg.setClasspathList(lst);
gse.setConfig(cfg);

def gcl = gse.getGroovyClassLoader();
child.getURLs().each { u-> gcl.addURL(u); }
def pcl = gse.getParentClassLoader();
child.getURLs().each { u-> pcl.addURL(u); }


Binding binding = new Binding();
binding.setVariable("args", myargs);
gse.run(f.getName(), binding);

def loadJars(path, arr = null) {
    def f = new File(path);
    if(debug) { println "loading jar from ${f.canonicalFile}"; }
    def vals;
    if(arr == null) {
        vals = [];
    } else {
        vals = arr;
    }

    f.eachFileMatch(~/.*\.jar/){ jar ->
        def url = jar.getCanonicalPath();
        //println "loading jar $url";
        vals << url.toString();
    }
    return vals;
}
def loadJarsToClassLoader(path, classloader) {
    def f = new File(path);
    if(debug) {println "loading jar from ${f.canonicalFile}"; }
    f.eachFileMatch(~/.*\.jar/){ jar ->
        def url = jar.getCanonicalFile().toURL();
        if(debug) { println "  loading jar $url"; }
        classloader.addURL(url);
    }
}

Jochen Theodorou

unread,
Jul 6, 2011, 2:34:48 PM7/6/11
to us...@groovy.codehaus.org
Am 06.07.2011 18:32, schrieb jean-philippe robichaud:
[...]

> The idea is to be able to do something like this:
> groovy wrapper.groovy path/to/my_groovy_script.groovy arg1 arg2 arg3...
>
> In the directory where wrapper.groovy is, there is a lib directory
> containing various jar files. Also, in the directory where
> my_groovy_script.groovy script resides, there is potentially some other
> groovy scripts/classes in the form a source files (with package name).
>
> Now the following wrapper.groovy code sorta works when I'm using my
> gentoo environment (where groovy 1.7.5 is compiled from sources using
> the magical ebuild from gentoo) but it doesn't work if I'm trying to use
> a binary distribution of groovy (1.7.x or 1.8).
>
> What am I doing wrong?

it would help if you can explain what goes wrong then in a bit more detail

[...]


> wrapper.groovy:
> import groovy.lang.Binding;
> import java.lang.reflect.*;
> import groovy.util.GroovyScriptEngine;
>
> def myargs = [];
> debug = true;
>
> args.eachWithIndex{a,i ->
> if(i != 0){
> myargs << a;
> }
> }

I think you could use here myargs = args[1..-1], but haven't tried it.

> GroovyScriptEngine gse = new GroovyScriptEngine(roots, child);
>
> def cfg = gse.getConfig();
> def lst = child.getURLs().collect{ it.toString()};
> cfg.setClasspathList(lst);
> gse.setConfig(cfg);
>
> def gcl = gse.getGroovyClassLoader();
> child.getURLs().each { u-> gcl.addURL(u); }
> def pcl = gse.getParentClassLoader();
> child.getURLs().each { u-> pcl.addURL(u); }

this step I don't get... normally the groovy class loader from the GSE
should be enough... and even that should not be needed as you give the
roots to GSE already

> def loadJars(path, arr = null) {
> def f = new File(path);
> if(debug) { println "loading jar from ${f.canonicalFile}"; }
> def vals;
> if(arr == null) {
> vals = [];
> } else {
> vals = arr;
> }
>
> f.eachFileMatch(~/.*\.jar/){ jar ->
> def url = jar.getCanonicalPath();
> //println "loading jar $url";
> vals << url.toString();
> }
> return vals;
> }

as a hint.... you do arr=null as optional parameter value, just to later
test for the null value and then give a different one in that case.
maybe arr=[] instead would work for you

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead
http://blackdragsview.blogspot.com/
For Groovy programming sources visit http://groovy.codehaus.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


jean-philippe robichaud

unread,
Jul 6, 2011, 4:13:01 PM7/6/11
to us...@groovy.codehaus.org
thanks for your quick reply and your hints!

On Wed, Jul 6, 2011 at 2:34 PM, Jochen Theodorou <blac...@gmx.org> wrote:
Am 06.07.2011 18:32, schrieb jean-philippe robichaud:
[...]
What am I doing wrong?

it would help if you can explain what goes wrong then in a bit more detail

Thanks, you're right, I didn't even explain what happens!   I've attached a complete example with 2 different wrapper scripts to help.  The attached zip file contains the following:
testcase/wrapper.v2.groovy
testcase/wrapper.v1.groovy
testcase/test.groovy
testcase/show_classloader.groovy
testcase/mypackage/DeepClass.groovy
testcase/lib/Jama-1.0.2.jar

On my gentoo box, if I use the 'system' groovy 1.7.5 install, the two following command works without trouble:
groovy wrapper.v1.groovy test.groovy 3
groovy wrapper.v2.groovy test.groovy 3

now if I unzip groovy-bin-1.8.0.zip and point GROOVY_HOME to the new "groovy-1.8.0" directory, doing:
$GROOVY_HOME/bin/groovy wrapper.v1.groovy test.groovy
or
$GROOVY_HOME/bin/groovy wrapper.v2.groovy test.groovy
dies with the beautiful message:

 -- start of err message --
loading jar from /tmp/testcase/lib
  loading jar file:/tmp/testcase/lib/Jama-1.0.2.jar
child class loader :
   => file:/tmp/testcase
   => file:/tmp
   => file:/tmp/testcase/lib/Jama-1.0.2.jar

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: startup failed:
mypackage.DeepClass: 3: unable to resolve class Jama.Matrix
 @ line 3, column 1.
   import Jama.Matrix;
   ^

1 error


org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
mypackage.DeepClass: 3: unable to resolve class Jama.Matrix
 @ line 3, column 1.
   import Jama.Matrix;
   ^

1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:854)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:493)
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:283)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:263)
        at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:777)
        at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:737)
        at groovy.lang.GroovyClassLoader$InnerLoader.loadClass(GroovyClassLoader.java:449)
        at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:793)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
        at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:696)
        at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:564)
        at org.codehaus.groovy.control.ResolveVisitor.resolveToClass(ResolveVisitor.java:709)
        at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:275)
        at org.codehaus.groovy.control.ResolveVisitor.resolveFromModule(ResolveVisitor.java:648)
        at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:275)
        at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:243)
        at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:227)
        at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:239)
        at org.codehaus.groovy.control.ResolveVisitor.transformConstructorCallExpression(ResolveVisitor.java:1069)
        at org.codehaus.groovy.control.ResolveVisitor.transform(ResolveVisitor.java:754)
        at org.codehaus.groovy.control.ResolveVisitor.transformDeclarationExpression(ResolveVisitor.java:1110)
        at org.codehaus.groovy.control.ResolveVisitor.transform(ResolveVisitor.java:746)
        at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitExpressionStatement(ClassCodeExpressionTransformer.java:139)
        at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
        at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:165)
        at org.codehaus.groovy.control.ResolveVisitor.visitBlockStatement(ResolveVisitor.java:1337)
        at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
        at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitConstructorOrMethod(ClassCodeExpressionTransformer.java:50)
        at org.codehaus.groovy.control.ResolveVisitor.visitConstructorOrMethod(ResolveVisitor.java:173)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
        at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1056)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
        at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1278)
        at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:148)
        at org.codehaus.groovy.control.CompilationUnit$8.call(CompilationUnit.java:601)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:839)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:493)
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
        at groovy.util.GroovyScriptEngine$ScriptClassLoader.parseClass(GroovyScriptEngine.java:186)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:267)
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:214)
        at groovy.util.GroovyScriptEngine.loadScriptByName(GroovyScriptEngine.java:459)
        at groovy.util.GroovyScriptEngine.createScript(GroovyScriptEngine.java:528)
        at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:515)
        at groovy.util.GroovyScriptEngine$run.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
        at wrapper.v1.run(wrapper.v1.groovy:45)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
        at groovy.lang.GroovyShell.run(GroovyShell.java:229)
        at groovy.lang.GroovyShell.run(GroovyShell.java:159)
        at groovy.ui.GroovyMain.processOnce(GroovyMain.java:514)
        at groovy.ui.GroovyMain.run(GroovyMain.java:329)
        at groovy.ui.GroovyMain.process(GroovyMain.java:315)
        at groovy.ui.GroovyMain.processArgs(GroovyMain.java:112)
        at groovy.ui.GroovyMain.main(GroovyMain.java:93)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
        at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)

1 error

--- end of stacktrace ---

What I find really strange is that I get the following outputs:

using the 'system' groovy 1.7.5

/tmp/testcase $ groovy wrapper.v1.groovy show_classloader.groovy
Class Loader of test.groovy has:
file:/tmp/testcase/

/tmp/testcase $ groovy wrapper.v2.groovy show_classloader.groovy
loading jar from /tmp/testcase/lib
loading jar from /tmp/testcase/lib
  loading jar file:/tmp/testcase/lib/Jama-1.0.2.jar
child class loader :
   => file:/tmp/testcase
   => file:/tmp
   => file:/tmp/testcase/lib/Jama-1.0.2.jar
Class Loader of test.groovy has:
file:/tmp/testcase/
file:/tmp/
file:/tmp/testcase/lib/Jama-1.0.2.jar
file:/tmp/testcase
file:/tmpthe 'system' groovy 1.7.5:


using the binary distribution of groovy 1.8:
$ $GROOVY_HOME/bin/groovy wrapper.v1.groovy show_classloader.groovy
Class Loader of test.groovy has:
file:/tmp/testcase/

/tmp/testcase $ $GROOVY_HOME/bin/groovy wrapper.v2.groovy show_classloader.groovy
loading jar from /tmp/testcase/lib
loading jar from /tmp/testcase/lib
  loading jar file:/tmp/testcase/lib/Jama-1.0.2.jar
child class loader :
   => file:/tmp/testcase
   => file:/tmp
   => file:/tmp/testcase/lib/Jama-1.0.2.jar
Class Loader of test.groovy has:
file:/tmp/testcase/
file:/tmp/
file:/tmp/testcase/lib/Jama-1.0.2.jar
file:/tmp/testcase
file:/tmp

Really, I don't understand what's happening, your help is greatly appreciated.

Thanks.

Jp


testcase.zip

Daniel Henrique Alves Lima

unread,
Jul 6, 2011, 9:57:14 PM7/6/11
to us...@groovy.codehaus.org
I think I've seen something like this before: Is it possible that
test.groovy is being loaded by the RootLoader instead of the child
classloader (because the current dir is always part of the classpath)?

Try the following: Move test.groovy to a subdirectory and add the
url of the subdirectory to your child classloader.
Something like:

testcase/wrapper.v2.groovy
testcase/wrapper.v1.groovy
testcase/child_dir/test.groovy


URLClassLoader child = new URLClassLoader (rootsURLs ,
this.getClass().getClassLoader());

child.addURL(new File('testcase/child_dir').toURI().toURL())


// Don't change the path here
groovy wrapper.v1.groovy test.groovy 3


Another newbie question: Why are you using URLClassLoader instead of
GroovyClassLoader?

Best regards,

Daniel.

---------------------------------------------------------------------

Daniel Henrique Alves Lima

unread,
Jul 6, 2011, 10:53:00 PM7/6/11
to us...@groovy.codehaus.org
Hum... For simple scripts it just works, no matter what.

Jean Philippe, run this test app as it is.
After that, copy Jama-1.0.2.jar to the base dir, add it to the urls
var (def urls = ['.', 'child_dir', 'Jama-1.0.2.jar']...), add the import
clause (import Jama.Matrix) to both scripts and run the tests again.

It works here for groovy 1.7.5 and 1.8.0 (I'm using CentOS).

==========

// run.groovy
def urls = ['.', 'child_dir'].collect{it -> new
File(it).toURI().toURL()} as URL[]
//def childCl = new java.net.URLClassLoader(urls, this.class.classLoader)
//def childCl = new GroovyClassLoader(this.class.classLoader)

def gse = new GroovyScriptEngine(urls)
//def gse = new GroovyScriptEngine(urls, childCl)

Binding binding = new Binding()

binding.setVariable('args', args)
gse.run('script1.groovy', binding)
gse.run('script2.groovy', binding)

===============
[daniel@techdm test_gse]$ groovy -version
Groovy Version: 1.8.0 JVM: 1.6.0_23
[daniel@techdm test_gse]$ groovy run.groovy
class script1 groovy.lang.GroovyClassLoader$InnerLoader@27b03c1a
groovy.util.GroovyScriptEngine$ScriptClassLoader@53fcc0a2
class script2 groovy.lang.GroovyClassLoader$InnerLoader@60ded0f0
groovy.util.GroovyScriptEngine$ScriptClassLoader@53fcc0a2

[daniel@techdm test_gse]$ groovy -version
Groovy Version: 1.7.5 JVM: 1.6.0_23
[daniel@techdm test_gse]$ groovy run.groovy
class script1 groovy.lang.GroovyClassLoader$InnerLoader@5dd2b9b7
groovy.util.GroovyScriptEngine$ScriptClassLoader@51e67ac
class script2 groovy.lang.GroovyClassLoader$InnerLoader@6f57b46f
groovy.util.GroovyScriptEngine$ScriptClassLoader@51e67ac

test_gse.zip

jean-philippe robichaud

unread,
Jul 6, 2011, 10:53:52 PM7/6/11
to us...@groovy.codehaus.org
Thanks for your suggestions Daniel,

I used URLClassLoader simply because that's the examples I had found on the web.  Sadly, I tried your suggestions with both the URLClassLoader and GroovyClassLoader without success.

The errors I got were exactly the same as before...

I'm ready to try something else!

Jp

jean-philippe robichaud

unread,
Jul 6, 2011, 10:57:41 PM7/6/11
to us...@groovy.codehaus.org
wow...  I wonder about one thing: could you download a binary distribution of groovy (http://dist.groovy.codehaus.org/distributions/groovy-binary-1.8.0.zip), unzip it, set GROOVY_HOME to the unzipped directory and rerun your tests using $GROOVY_HOME/bin/groovy run.groovy?

I'll try your example


Thanks for your help!

Jp

Daniel Henrique Alves Lima

unread,
Jul 6, 2011, 11:05:05 PM7/6/11
to us...@groovy.codehaus.org
I meant 'run *the attached app* as it is.'

---------------------------------------------------------------------

Daniel Henrique Alves Lima

unread,
Jul 6, 2011, 11:20:36 PM7/6/11
to us...@groovy.codehaus.org
I just did it. I'm using groovy groovy 1.8.0 from my tmp dir :-)


[daniel@techdm test_gse]$ echo $GROOVY_HOME ; which groovy ; groovy -version
/usr/local/groovy-1.7.5/
/usr/local/groovy-1.7.5/bin/groovy


Groovy Version: 1.7.5 JVM: 1.6.0_23


[daniel@techdm test_gse]$ echo $GROOVY_HOME ; which groovy ; groovy -version
/tmp/groovy-1.8.0/
/tmp/groovy-1.8.0/bin/groovy


Groovy Version: 1.8.0 JVM: 1.6.0_23

jean-philippe robichaud

unread,
Jul 6, 2011, 11:24:54 PM7/6/11
to us...@groovy.codehaus.org
wow, that's working...  I'm confused...

Here's what I did:
1) I modified run.groovy so that it now reads as:
def urls = ['.', 'child_dir', 'lib/Jama-1.0.2.jar'].collect{it -> new File(it).toURI().toURL()} as URL[]

def gse = new GroovyScriptEngine(urls)
Binding binding = new Binding()
binding.setVariable('args', args)
gse.run('script1.groovy', binding)
gse.run('script2.groovy', binding)
gse.run('script3.groovy', binding)

2) I created a directory called "mypackage" and I've put DeepClass.groovy in it:
// mypackage/DeepClass.groovy
package mypackage;
import Jama.Matrix
class DeepClass {
    def show () {
        println "${this.class} ${this.class.classLoader} ${this.class.classLoader.parent}"
    }
}
3) I created a "lib" directory and I've put Jama-1.0.2.jar  into it
4) I created script3.groovy in the same directory as run.groovy:
//script3.groovy:
import Jama.Matrix
import mypackage.*;

println "${this.class} ${this.class.classLoader} ${this.class.classLoader.parent}"
def deep = new DeepClass();
deep.show();


test_gse $ groovy run.groovy:
class script1 groovy.lang.GroovyClassLoader$InnerLoader@3c3228a1 groovy.util.GroovyScriptEngine$ScriptClassLoader@5a92668c
class script2 groovy.lang.GroovyClassLoader$InnerLoader@11d13272 groovy.util.GroovyScriptEngine$ScriptClassLoader@5a92668c
class script3 groovy.lang.GroovyClassLoader$InnerLoader@fc8fb4b groovy.util.GroovyScriptEngine$ScriptClassLoader@5a92668c
class mypackage.DeepClass groovy.lang.GroovyClassLoader$InnerLoader@fc8fb4b groovy.util.GroovyScriptEngine$ScriptClassLoader@5a92668c

test_gse $ $GROOVY_HOME/bin/groovy run.groovy
class script1 groovy.lang.GroovyClassLoader$InnerLoader@700a4488 groovy.util.GroovyScriptEngine$ScriptClassLoader@3a51127a
class script2 groovy.lang.GroovyClassLoader$InnerLoader@298488ef groovy.util.GroovyScriptEngine$ScriptClassLoader@3a51127a
class script3 groovy.lang.GroovyClassLoader$InnerLoader@606e1dec groovy.util.GroovyScriptEngine$ScriptClassLoader@3a51127a
class mypackage.DeepClass groovy.lang.GroovyClassLoader$InnerLoader@606e1dec groovy.util.GroovyScriptEngine$ScriptClassLoader@3a51127a

I'll try to create a new wrapper script based on this, hopefully, it won't break (or we'll find where it will). 

Million thanks!

Jp

jean-philippe robichaud

unread,
Jul 6, 2011, 11:47:46 PM7/6/11
to us...@groovy.codehaus.org
Thanks Daniel, it's working perfectly now.

I'm attaching the final version, hopefully, it could be useful to others as well

Good night,

Jp
wrapper.v3.groovy

Daniel Henrique Alves Lima

unread,
Jul 7, 2011, 10:39:56 AM7/7/11
to us...@groovy.codehaus.org
No problem.

For documentation purpose only, I've attached that case about
the RootLoader. I think the script2 is loaded by the child
classLoader, but the script1 is loaded by the rootLoader. That's why
the script1 fails when accessing blah.X class. I suppose this won't
happen when using GSE.


============

// run.groovy
def urls = ['.', 'child_dir', 'lib/blah.jar'].collect{it -> new
File(it).toURI().toURL()} as URL[] def childCl = new
GroovyClassLoader(this.class.classLoader)
for (url in urls) {childCl.addURL(url)}

for (scriptName in ['script2', 'script1']) {
def script = childCl.loadClass(scriptName, true, false)
script = script.newInstance()
script.args = args
script.run()
println ''
}

===============

C:\tmp\test_child_cl>groovy run.groovy
class script2 groovy.lang.GroovyClassLoader$InnerLoader@1dfc8a0
groovy.lang.GroovyClassLoader@15ccfb1
class blah.X
Doing something

class script1 groovy.lang.GroovyClassLoader$InnerLoader@1f8d077
groovy.lang.GroovyClassLoader@2e323
Caught: groovy.lang.MissingPropertyException: No such property: blah for class:
script1
Possible solutions: class
at script1.run(script1:2)
at run.run(run.groovy:9)

C:\tmp\test_child_cl>

test_child_cl.tar.bz2
Reply all
Reply to author
Forward
0 new messages