# Running Groovy from the commandline

version 4.0.15

## 1. groovy, the Groovy command

`groovy` invokes the Groovy command line processor. It allows you to run inline Groovy expressions, and scripts, tests or application within groovy files. It plays a similar role to `java` in the Java world but handles inline scripts and rather than invoking class files, it is normally called with scripts and will automatically call the Groovy compiler as needed.

The easiest way to run a Groovy script, test or application is to run the following command at your shell prompt:

    > groovy MyScript.groovy

The `.groovy` part is optional. The `groovy` command supports a number of command line switches:

Short version | Long version | Description | Example
--- | --- | --- | ---
-a | --autosplit <splitPattern> | split lines using splitPattern (default '\s') using implicit 'split' variable |
-b | --basescript <class> | Base class name for scripts (must derive from Script) |
-c | --encoding <charset> | specify the encoding of the files |
-cp <path> | -classpath <path> --classpath <path> | Specify the compilation classpath. Must be the first argument. | groovy -cp lib/dep.jar MyScript
| --configscript <path> | Advanced compiler configuration script | groovy --configscript config/config.groovy src/Person.groovy
-D | --define <name=value> | define a system property |
-d | --debug | debug mode will print out full stack traces |
| --disableopt <optlist> | disables one or all optimization elements. optlist can be a comma separated list with the elements: all (disables all optimizations), int (disable any int based optimizations) |
-e <script> |  | specify an inline command line script | groovy -e "println new Date()"
-h | --help | Displays usage information for the command line groovy command | groovy --help
-i <extension> |  | modify files in place; create backup if extension is given (e.g. '.bak') |
-l <port> |  | listen on a port and process inbound lines (default: 1960) |
-n |  | process files line by line using implicit 'line' variable |
-p |  | process files line by line and print result (see also -n) |
-v | --version | display the Groovy and JVM versions | groovy -v
-pa | --parameters | Generates metadata for reflection on method parameter names on JDK 8 and above. Defaults to false. | groovy --parameters Person.groovy
-pr | --enable-preview | Enable preview Java features (jdk12+ only). | groovy --enable-preview Person.groovy