Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass command-line arguments to Groovy script

Tags:

groovy

I have a simple Groovy script like this:

class Main {
    static void main(String... args) {
        args.each {
            println ${it}
        }
    }
}

I cannot figure out how to pass command line arguments to it. I'm on Windows and run the script as:

Groovy myscript.groovy "arg1" "arg2"

And it results in error:

Caught: groovy.lang.MissingMethodException: No signature of method: Main$_main_closure1.$() is applicable for argument types: (Main$_main_closure1$_closure2) values: [Main$_main_closure1$_closure2@68b32e3e] Possible solutions: is(java.lang.Object), is(java.lang.Object), any(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;) groovy.lang.MissingMethodException: No signature of method: Main$_main_closure1.$() is applicable for argument types: (Main$_main_closure1$_closure2) values: [Main$_main_closure1$_closure2@68b32e3e] Possible solutions: is(java.lang.Object), is(java.lang.Object), any(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;) at Main$_main_closure1.doCall(hash_text.groovy:10) at Main.main(hash_text.groovy:9)

like image 876
Cortlendt Avatar asked Nov 20 '25 15:11

Cortlendt


1 Answers

you have just a syntax error. instead of

println ${it}

use

println it

or

println "${it}"
like image 102
daggett Avatar answered Nov 22 '25 05:11

daggett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!