Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a groovy code be compiled to run in JRE?

Tags:

groovy

I am new to groovy and I cannot understand, if it is possible to compile a groovy program, so it runs at all computers, were the JRE is installed.

The application I am developing has to run on any computer with JRE 1.5. Is it possible to start using groovy and maintain this flexibility? With JRE 1.6?

I have heard about the library groovy-all-VERSION.jar. Is this the one required library to be shipped with my application?

like image 879
Andrej Avatar asked Apr 30 '11 22:04

Andrej


People also ask

Does Groovy compile to Java?

Groovy scripts can use any Java classes. They can be compiled to Java bytecode (in . class files) that can be invoked from normal Java classes. The Groovy compiler, groovyc, compiles both Groovy scripts and Java source files, however some Java syntax (such as nested classes) is not supported yet.

Can Java run Groovy script?

With the GroovyClassLoader we can load Groovy scripts and run them in Java code. First we must create a new GroovyClassLoader and then parse a Groovy script. The script can be in a file, string or inputstream. Once the script is parsed we have a Class and we can make a new instance of this class.

Does Groovy get compiled?

Unlike Java, a Groovy source code file can be executed as an (uncompiled) script, if it contains code outside any class definition, if it is a class with a main method, or if it is a Runnable or GroovyTestCase. A Groovy script is fully parsed, compiled, and generated before executing (similar to Python and Ruby).

Is it possible to have a Groovy and Java code in one project?

Joint compilation is a process designed to compile both Java and Groovy files in the same project, in a single Maven command. With joint compilation, the Groovy compiler will: parse the source files.


1 Answers

The answer is yes. In fact, all groovy code compiles down to Java classes that run on the JRE. All you need is JRE 1.4 or higher and the groovy-all-*.jar on the classpath of your application.

Since you are looking to support JRE 1.5 or higher, make sure your source compatibility is set on your compiler to this level.

There are a few options for compiling your groovy code. Groovyc (Ant Task), GMaven (Maven) and Gradle are all options.

Another option you have is to 'not' compile your groovy code. The groovy distribution only requires the JRE to be installed. You can ship your application as a set of scripts that can simply be run using the groovy install. It depends on how sensitive your source code is.

like image 172
Chris Dail Avatar answered Oct 13 '22 13:10

Chris Dail