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.
views:
2740answers:
4
+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
2008-11-20 17:39:19
Heh, definitely missed the "Adding things to the classpath" section first time I read that.
timdisney
2008-11-20 17:43:02
I could not get that classloader to work... anyone else able to use it?
Zombies
2010-04-23 15:18:35
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
2008-11-20 17:40:18
+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
2008-12-01 19:54:07