[groovy-user] dynamic loading script file - no such property: error

17 views
Skip to first unread message

prabuinet

unread,
Dec 10, 2009, 1:51:38 AM12/10/09
to us...@groovy.codehaus.org

Hi!

I'm loading a groovy script file dynamically with GroovyClassLoader
In the script file I have this code

def var = false;

void run() {
if(var == false) {
...
}
}


though I have declared the variable "var" in the script file I'm getting the
error when call the function run().

Caught: groovy.lang.MissingPropertyException: No such property: var for
class: foo


Thanks.
--
View this message in context: http://old.nabble.com/dynamic-loading-script-file---no-such-property%3A-error-tp26722825p26722825.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

http://xircles.codehaus.org/manage_email


Merlyn Albery-Speyer

unread,
Dec 10, 2009, 2:15:18 AM12/10/09
to us...@groovy.codehaus.org
I get that error if I run this script:

def var = false
boolean run() {
return (var == false)
}
assert run() == true

What works for me is:

def var = false
def run = {
return (var == false)
}
assert run() == true

Does that help at all?

On Wed, Dec 9, 2009 at 10:51 PM, prabuinet <prab...@gmail.com> wrote:
>
> Hi!
>
> I'm loading a groovy script file dynamically with GroovyClassLoader
> In the script file I have this code
>
> def var = false;
>
> void run() {
>   if(var == false) {
>       ...
>   }
> }
>
>
> though I have declared the variable "var" in the script file I'm getting the
> error when call the function run().
>
> Caught: groovy.lang.MissingPropertyException: No such property: var for
> class: foo

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

prabuinet

unread,
Dec 10, 2009, 2:47:05 AM12/10/09
to us...@groovy.codehaus.org

Now I'm getting a different error:

Caught: groovy.lang.MissingMethodException: No signature of method:
foo.run() is applicable for argument types: () values: {}

--
View this message in context: http://old.nabble.com/dynamic-loading-script-file---no-such-property%3A-error-tp26722825p26723279.html


Sent from the groovy - user mailing list archive at Nabble.com.

Roshan Dawrani

unread,
Dec 10, 2009, 3:27:45 AM12/10/09
to us...@groovy.codehaus.org
Please refer http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22 to find out why a variable declared using "def" is not visible in other methods.

prabuinet

unread,
Dec 10, 2009, 4:23:21 AM12/10/09
to us...@groovy.codehaus.org

def var = false;

void run() {
if(var == false) {
...
}
}


Ooh! "var' is not in other module.. it is in the same script file.
--
View this message in context: http://old.nabble.com/dynamic-loading-script-file---no-such-property%3A-error-tp26722825p26724198.html

Andreas Jöcker

unread,
Dec 10, 2009, 5:11:58 AM12/10/09
to us...@groovy.codehaus.org
Hi all,

I have the following config file (build.properties) :

target = foo
source = bar-buh

the following code works intentionally:

def props = new Properties()
props.load(new FileInputStream(new File('build.properties')))
def config = new ConfigSlurper().parse(props)
println config.source

the following code fails:

def c = new ConfigSlurper().parse(new File('build.properties').toURL())
println c.target

Exception in thread "main" groovy.lang.MissingMethodException: No
signature of method: groovy.util.ConfigObject.minus() is applicable for
argument types: (groovy.util.ConfigObject) values: {[:]}
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:172)
at script1260439847206.run(script1260439847206.groovy:2)

Shouldn't these two ways of loading a configuration be equal ?

Thx
Andreas

--

Andreas Jöcker
GiS - Gesellschaft für integrierte Systemplanung mbH
Junkersstr. 2
69469 Weinheim

E-Mail a.jo...@gis-systemhaus.de
Telefon +49 6201 503-59
Fax +49 6201 503-66

Gesellschaft für integrierte Systemplanung mbH
Geschäftsführer: Eckhard Haffmann, Alfred Gai, Bernd Heselmann
Sitz der Gesellschaft: Zeppelinstr. 11 - 91052 Erlangen
Amtsgericht Fürth/Bayern - Handelsregister-Nr. HRB 3435

Andreas Jöcker

unread,
Dec 10, 2009, 5:14:43 AM12/10/09
to us...@groovy.codehaus.org
:(
sorry - I guess I respond an existing thread with a new question again...

sorry for messing up someones view

Jochen Theodorou

unread,
Dec 10, 2009, 8:46:50 AM12/10/09
to us...@groovy.codehaus.org
prabuinet schrieb:

> def var = false;
>
> void run() {
> if(var == false) {
> ...
> }
> }
>
>
> Ooh! "var' is not in other module.. it is in the same script file.

the same script file, but not the same method. var is a local variable
to the implicit run method, but then you even overwrite the run method.
So your code cannot work even if you removed the "def" in front of "def
var" and thus making it a value stored in the binding rather than a
local variable that is invisible in other methods.

bye blackdrag

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

Reply all
Reply to author
Forward
0 new messages