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
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
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.
---------------------------------------------------------------------
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
---------------------------------------------------------------------
[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
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>