Running Groovy on the Command Line

Of course, for some programmers nothing can give as much pleasure as getting into the command line and running the program from there. We can do that by typing the command groovy followed by the Groovy program filename, as shown next.

 
> cat Hello.groovy
 
println "Hello Groovy!"
 
> groovy Hello
 
Hello Groovy!
 
>

To try a couple of statements directly on the command line, use the -e option. Type groovy -e "println ’hello’" on the command line, and press Enter/Return. Groovy will output “hello.”

Realistically, though, the groovy command is useful for executing large Groovy scripts and classes. It expects us to either have some executable code outside any class, or have a class with a static main(String[] ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.