views:

2740

answers:

4

I have a groovy script that needs a library in a jar. How do I add that to the classpath? I want the script to be executable so I'm using #!/usr/bin/env groovy at the top of my script.

+5  A: 

Are none of the options listed at http://groovy.codehaus.org/Running working for you?

Give them a try if you haven't.

If you really have to you can also load a JAR at runtime with:

this.getClass().classLoader.rootLoader.addURL(new File("file.jar").toURL())
Eric Wendelin
Heh, definitely missed the "Adding things to the classpath" section first time I read that.
timdisney
I could not get that classloader to work... anyone else able to use it?
Zombies
A: 

The same as you would in Java.

This is an example of running a MySQL status monitoring script. mysql.jar contains the MySQL connector that I call from script status.groovy.

groovy -cp mysql.jar status.groovy ct1

tessein
+1  A: 

You can add the jars to $HOME/.groovy/lib

Ove S
+1  A: 

You can also try out Groovy Grape. It lets you use annotations to modify the classpath. Its experimental right now, but pretty cool. See http://groovy.codehaus.org/Grape

Bob Herrmann