Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an executable jar with groovy script

Tags:

jar

groovy

I have a groovy script Automate.groovy. The script does not have a class definition or a main method. I am looking for the simplest way to create a jar containing the script. I tried compiling the groovy script using the following command:

groovyc Automate.groovy

I also have a Manifest file manifest-addition.txt that specifies the main class

Main-Class: Automate

And I created the jar using the command:

jar cvfm Automate.jar mainfest-addition.txt *.class

This gave me a jar with Automate.class and Automate$1.class and a Manifest file with the Main class. But when I tried to run the jar using java -jar Automate.jar I always get the error

cannot find or load main class Automate.

What am I doing wrong here?

There should be a simple way like this to create the jar. I just have this one script file that I want in my jar and I do not want to use maven/ant/gradle to get this job done. Isn't there a simple and straightforward way to do this?

like image 594
Harini Avatar asked Jan 18 '16 23:01

Harini


Video Answer


1 Answers

The class file has dependencies on Groovy. Using the embeddable jar will fix the problem.

Using Groovy 2.4.5, if I have this script for Automate.groovy

println "Hello from Automate.groovy"

and use this manifest.txt:

Main-class: Automate
Class-path: jar/groovy-all-2.4.5.jar

and this simple build script:

rm *.class
groovyc Automate.groovy
jar cvfm Automate.jar manifest.txt Automate.class

and, in the run directory, I perform this:

bash$ mkdir jar
bash$ cp $GROOVY_HOME/embeddable/groovy-all-2.4.5.jar jar

then this works:

bash$ java -jar Automate.jar 
Hello from Automate.groovy

The solution will be much better if the embeddable jar is included in the Automate.jar itself. An example of that, using Gradle, is this project.

like image 167
Michael Easter Avatar answered Nov 15 '22 07:11

Michael Easter