Sunday, 13 January 2019

Groovy: Tools to execute groovy


Groovy provides 3 command line tools to work with Groovy.

a.   groovysh : It is a command line shell used to work with groovy interactively.
b.   groovyconsole : It starts a graphical user interface to execute groovy statements, load groovy script files and execute them.
c.    groovy : It is an interpreter to execute groovy scripts.

groovysh
Open terminal (or) command prompt and execute ‘groovysh’ command. It opens groovy interactive shell.

C:\>groovysh
Groovy Shell (2.5.3, JVM: 1.8.0_171)
Type ':help' or ':h' for help.
--------------------------------------------------------------------------------------------------------------------------------------
groovy:000>


You can evaluate the expressions in groovy prompt.

groovy:000> 20 + 30
===> 50
groovy:000> 20 - 30
===> -10
groovy:000> 20 * 30
===> 600
groovy:000> 20 / 30
===> 0.6666666667


You can even define the variables and access them in groovy console.

groovy:000> message = "Hello World to Groovy language"
===> Hello World to Groovy language
groovy:000> message
===> Hello World to Groovy language


Execute the command :help to get list of all available commands.

groovy:000> :help

For information about Groovy, visit:
    http://groovy-lang.org

Available commands:
  :help      (:h ) Display this help message
  ?          (:? ) Alias to: :help
  :exit      (:x ) Exit the shell
  :quit      (:q ) Alias to: :exit
  import     (:i ) Import a class into the namespace
  :display   (:d ) Display the current buffer
  :clear     (:c ) Clear the buffer and reset the prompt counter
  :show      (:S ) Show variables, classes or imports
  :inspect   (:n ) Inspect a variable or the last result with the GUI object browser
  :purge     (:p ) Purge variables, classes, imports or preferences
  :edit      (:e ) Edit the current buffer
  :load      (:l ) Load a file or URL into the buffer
  .          (:. ) Alias to: :load
  :save      (:s ) Save the current buffer to a file
  :record    (:r ) Record the current session to a file
  :history   (:H ) Display, manage and recall edit-line history
  :alias     (:a ) Create an alias
  :set       (:= ) Set (or list) preferences
  :grab      (:g ) Add a dependency to the shell environment
  :register  (:rc) Register a new command with the shell
  :doc       (:D ) Open a browser window displaying the doc for the argument

For help on a specific command type:
    :help command

groovyconsole
‘groovyconsole’ command opens a swing based graphical user interface, it is used to execute groovy script.


Open command prompt or terminal and execute the command ‘groovyconsole’.



Write groovy statement and run the script. Script -> Run



Once you click on ‘Run’ button, you can able to see the output in console.
groovy
Groovy provides an interpreter ‘groovy’, it is a command line tool to execute groovy scripts.

HelloWorld.groovy
int a = 10
int b = 20

print "Sum of 10 and 20 is ${a + b}"

Run HelloWorld.groovy application by executing the command 'groovy HelloWorld.groovy'.

Output
Sum of 10 and 20 is 30

You can omit the '.groovy' extension while executing groovy script. You can execute the above file using below command also.

groovy HelloWorld


Previous                                                 Next                                                 Home

No comments:

Post a Comment