Compiling and Running Groovy

We have used Groovy in direct mode. In this part, there is another way of using Groovy  is to compile it to Java byte code and running it as regular Java application code within a Java Virtual Machine (JVM). This is called precompiled mode. Both ways execute Groovy inside a JVM eventually, and both ways compile.

Compiling Groovy with groovyc :

Groovy works with groovyc compiler. The groovyc compiler generates at least one class file for each Groovy source file compiled. Groovy compiler outputs two Java class files to a directory named classes. When the compiler is running, the name of each generated class file is printed to the console. Groovyc generates a class for each script. The name of the compiled class matches the name of the script being compiled. The number of the classes generated depending on the script code.

Classes generated by  Groovyc for the Fibonacci.groovy file is shown by table:

Class file Subclass Purpose
Fibonacci.class groovy.lang.

Script

 

Main method that can be run with the java command.

 

Fibonacci$_run_

closure1.class

groovy.lang.

Closure

 

Captures what has to be done 10

times.

You can safely ignore it.

 

 

Running a compiled Groovy script with Java:

A compiled Groovy program is same as running a compiled Java program. with the requirement of having the embeddable groovy-all*.jar file in JVM’s classpath, which will ensure that all of Groovy’s third-party dependencies will be resolved automatically at runtime. You have to add the directory in which compiled program resides to the classpath. Then run the program in the same way you would run any other Java program.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.