7

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?

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why is it a better solution to include the groovy-all jar inside Automate.jar?
I actually tried including groovy-all jar in classpath using java -cp groovy-all.jar Automate.jar. Even then I got the same error. I actually had a @Grab annotation and that is what was actually causing the problem. When I removed it, it was able to locate the main class
@Harini to include it with -classpath you would need to add it in addition to the jar file, like: java -cp Automate.jar:groovy-all-2.4.5.jar Automate
-1

The actual problem was the @Grab Annotation that was there in my script. I created a Gradle project and added a very simple script HelloWorld.groovy with just one println statement but also used a @Grab to just import something. When I built the jar it would not find the main class. When I googled around I found that the @Grab annotation is meant to be used in standalone scripts and not for pre-compiled script. So if you need to have @Grab in your compiled groovy then you need to add an additional ivy dependency or choose not to have the @Grab and use some other way to include dependencies. I removed @Grab from my script and then built it. The jar executed fine then.

Sorry btw for deviating from the fact that I did not want to use Gradle/Maven. I almost spent two days trying all the things that I could find on stackoverflow and google and ended up trying Gradle. I am not sure if this is the only solution to the problem but this worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.