Kallin Nagelberg | 16 Jul 16:04

[groovy-user] rootloader scope

I'm trying to write a groovy script that depends on some libraries in ../lib.

I do this:

def loader = this.class.classLoader.rootLoader
def dir = new File('../lib')
dir.eachFileMatch(~/.*\.jar$/) {
    loader.addURL(it.toURL())
}

but the class I'm looking for is not found by the script.

However, when I now do an 'evaluate' of another script, that script has no problems finding classes from the libraries.
Is there a way affect the classpath of the current script?
Jochen Theodorou | 16 Jul 20:13

Re: [groovy-user] rootloader scope

Kallin Nagelberg schrieb:
> I'm trying to write a groovy script that depends on some libraries in 
> ../lib.
> 
> I do this:
> 
> def loader = this.class.classLoader.rootLoader
> def dir = new File('../lib')
> dir.eachFileMatch(~/.*\.jar$/) {
>     loader.addURL(it.toURL())
> }
> 
> but the class I'm looking for is not found by the script.
> 
> However, when I now do an 'evaluate' of another script, that script has 
> no problems finding classes from the libraries.
> Is there a way affect the classpath of the current script?

what you can not do is the following:

def loader = this.class.classLoader.rootLoader
loader.addURL("myJar.jar")
def x  = Foo.x // Foo is from myJar.jar

this won't work, because at the point where the jar is added, the 
compiler did already decide that Foo is no class. The script needs a 
recompilation first. So any evaluate that follows will find the class, 
simply because the script therein is compiled after rootloader was used.

I am sure we could find a nice solution if you would give an example of 
the usage of the class, where it currently leads to a 
MissingPropertyException

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

    http://xircles.codehaus.org/manage_email

Kallin Nagelberg | 16 Jul 20:58

Re: [groovy-user] rootloader scope

Thanks for the info blackdrag

Well there are some utility methods in "myJar.jar" that I would like to use from a command line script.

From what I can tell i have to either:

1) Always start a single script that loads all the libraries, and then evaluates the desired script, like
groovy libloader thescriptIwanttorun

2) create a secondary script whenever I want to access something in my libraries, either inline or in a separate file.

Both are easy, but somewhat roundabout.
Can you suggest a more straightforward approach?

Thanks,
Kal.



On Wed, Jul 16, 2008 at 2:13 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Kallin Nagelberg schrieb:

I'm trying to write a groovy script that depends on some libraries in ../lib.

I do this:

def loader = this.class.classLoader.rootLoader
def dir = new File('../lib')
dir.eachFileMatch(~/.*\.jar$/) {
   loader.addURL(it.toURL())
}

but the class I'm looking for is not found by the script.

However, when I now do an 'evaluate' of another script, that script has no problems finding classes from the libraries.
Is there a way affect the classpath of the current script?


what you can not do is the following:


def loader = this.class.classLoader.rootLoader
loader.addURL("myJar.jar")
def x  = Foo.x // Foo is from myJar.jar

this won't work, because at the point where the jar is added, the compiler did already decide that Foo is no class. The script needs a recompilation first. So any evaluate that follows will find the class, simply because the script therein is compiled after rootloader was used.

I am sure we could find a nice solution if you would give an example of the usage of the class, where it currently leads to a MissingPropertyException

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

  http://xircles.codehaus.org/manage_email



Jochen Theodorou | 16 Jul 22:58

Re: [groovy-user] rootloader scope

Kallin Nagelberg schrieb:
> Thanks for the info blackdrag
> 
> Well there are some utility methods in "myJar.jar" that I would like to 
> use from a command line script.
> 
>  From what I can tell i have to either:
> 
> 1) Always start a single script that loads all the libraries, and then 
> evaluates the desired script, like
> groovy libloader thescriptIwanttorun
> 
> 2) create a secondary script whenever I want to access something in my 
> libraries, either inline or in a separate file.
> 
> Both are easy, but somewhat roundabout.
> Can you suggest a more straightforward approach?

well, depends... if you do not need the classes in your own method 
signatures, then just load them using the classloader...

loader.load("Foo").newInstance()

would load Foo and create a new instance of Foo.

If this scheme is not possible for you, then I am afraid you have to 
either wait till the small grape project is ready, or spin off a 
solution like 1 or 2 yourself

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

    http://xircles.codehaus.org/manage_email

Kallin Nagelberg | 16 Jul 23:02

Re: [groovy-user] rootloader scope

small grape? I'm goggling that as we speak :) Thanks!

On Wed, Jul 16, 2008 at 4:58 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Kallin Nagelberg schrieb:

Thanks for the info blackdrag

Well there are some utility methods in "myJar.jar" that I would like to use from a command line script.

 From what I can tell i have to either:

1) Always start a single script that loads all the libraries, and then evaluates the desired script, like
groovy libloader thescriptIwanttorun

2) create a secondary script whenever I want to access something in my libraries, either inline or in a separate file.

Both are easy, but somewhat roundabout.
Can you suggest a more straightforward approach?

well, depends... if you do not need the classes in your own method signatures, then just load them using the classloader...

loader.load("Foo").newInstance()

would load Foo and create a new instance of Foo.

If this scheme is not possible for you, then I am afraid you have to either wait till the small grape project is ready, or spin off a solution like 1 or 2 yourself


bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

  http://xircles.codehaus.org/manage_email



Kallin Nagelberg | 16 Jul 23:13

Re: [groovy-user] rootloader scope

I can find no small grape project. Is this a joke?

On Wed, Jul 16, 2008 at 4:58 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Kallin Nagelberg schrieb:

Thanks for the info blackdrag

Well there are some utility methods in "myJar.jar" that I would like to use from a command line script.

 From what I can tell i have to either:

1) Always start a single script that loads all the libraries, and then evaluates the desired script, like
groovy libloader thescriptIwanttorun

2) create a secondary script whenever I want to access something in my libraries, either inline or in a separate file.

Both are easy, but somewhat roundabout.
Can you suggest a more straightforward approach?

well, depends... if you do not need the classes in your own method signatures, then just load them using the classloader...

loader.load("Foo").newInstance()

would load Foo and create a new instance of Foo.

If this scheme is not possible for you, then I am afraid you have to either wait till the small grape project is ready, or spin off a solution like 1 or 2 yourself


bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

  http://xircles.codehaus.org/manage_email



Jochen Theodorou | 16 Jul 23:19

Re: [groovy-user] rootloader scope

Kallin Nagelberg schrieb:
> I can find no small grape project. Is this a joke?

kind of, sorry... I did mean 
http://docs.codehaus.org/display/GROOVY/Grapes+and+grab() it is not 
ready yet

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

    http://xircles.codehaus.org/manage_email

Kallin Nagelberg | 16 Jul 23:29

Re: [groovy-user] rootloader scope

very cool, i look forward to checking that out.

On Wed, Jul 16, 2008 at 5:19 PM, Jochen Theodorou <blackdrag-BA+cFGlbTmA@public.gmane.org> wrote:
Kallin Nagelberg schrieb:

I can find no small grape project. Is this a joke?

kind of, sorry... I did mean http://docs.codehaus.org/display/GROOVY/Grapes+and+grab() it is not ready yet


bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

  http://xircles.codehaus.org/manage_email



Pascal DeMilly | 16 Jul 23:43

Re: [groovy-user] rootloader scope


Grapes and Grab seems such a great addition to groovy. Is that already in 
trunk or how can we use it?

TIA

Pascal

On Wednesday 16 July 2008, Kallin Nagelberg wrote:
> very cool, i look forward to checking that out.
>
> On Wed, Jul 16, 2008 at 5:19 PM, Jochen Theodorou <blackdrag@...> wrote:
> > Kallin Nagelberg schrieb:
> >> I can find no small grape project. Is this a joke?
> >
> > kind of, sorry... I did mean
> > http://docs.codehaus.org/display/GROOVY/Grapes+and+grab()<http://docs.cod
> >ehaus.org/display/GROOVY/Grapes+and+grab%28%29>it is not ready yet
> >
> >
> > bye blackdrag
> >
> > --
> > Jochen "blackdrag" Theodorou
> > The Groovy Project Tech Lead (http://groovy.codehaus.org)
> > http://blackdragsview.blogspot.com/
> > http://www.g2one.com/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >   http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Jul 11:52

Re: [groovy-user] rootloader scope

Pascal DeMilly schrieb:
> Grapes and Grab seems such a great addition to groovy. Is that already in 
> trunk or how can we use it?

it is under http://svn.codehaus.org/groovy/trunk/groovy/modules/grape-ivy/

but I really can not say what the current state is, because I am not 
working on that myself.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

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

    http://xircles.codehaus.org/manage_email

Avi Flax | 17 Jul 04:20

Re: [groovy-user] rootloader scope

On Wed, Jul 16, 2008 at 2:58 PM, Kallin Nagelberg
<kallin.nagelberg <at> gmail.com> wrote:

> 1) Always start a single script that loads all the libraries, and then evaluates the desired script, like
> groovy libloader thescriptIwanttorun

Kallin, I thought this was a cool idea, so I took a crack at it:

::BEGIN_CODE::

/*** runscriptwithlibs.groovy ***/

if (args.length == 0) {
    println '''
* runscriptwithlibs.groovy requires at least one argument: the path to
the script to run.

* The second, optional argument is a colon-delimited list of paths of
directories to search for libraries.
  * If it is not supplied, the script will use the current directory.

* All specified directories will be *recursively* searched for JAR or
CLASS files to be added to the classpath.
'''
    return
}

def addDirToLoader(dir) {
    dir.eachFileMatch(~/.*\.jar$/) {
        this.class.classLoader.rootLoader.addURL(it.toURL())
    }

    dir.eachFileMatch(~/.*\.class$/) {
        this.class.classLoader.rootLoader.addURL(it.toURL())
    }

    dir.eachDir {
        addDirToLoader(it)
    }
}

/*** Add all the specified libs to the rootLoader ***/
if (args.length < 2 || args[1] == null || args[1].length() == 0)
    libDirs = "."
else
    libDirs = args[1].split(":")

libDirs.each {
    addDirToLoader(new File(it))
}

/*** Create the GroovyShell and run the script ***/
new GroovyShell().evaluate(new File(args[0]))

::END_CODE::

Works for me. Interestingly, since I'm running Unix, I can have a
script file that looks like this:

::BEGIN CODE::
#!/usr/bin/env groovy /Users/avi/bin/runscriptwithlibs.groovy
println some.class.that.is.in.some.jar.file.Class.class.name
::END CODE::

Then, I just make that file executable, and I can run it from
anywhere, and runscriptwithlibs.groovy doesn't need to be in the same
folder.

For example:

./myscript.groovy

(if libs are in the current dir, or any descendant of the current dir)

or

./myscript.groovy ../path/to/libs

I don't know if this'll be useful for anyone, but it was interesting
to work it out.

Thanks,
Avi

--

-- 
Avi Flax » Lead Technologist » Partner » Arc90 » http://arc90.com

Gmane