Friday, March 4, 2011

How do I include jars in a groovy script?

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.

From stackoverflow
  • 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())
    
    timdisney : Heh, definitely missed the "Adding things to the classpath" section first time I read that.
    Zombies : I could not get that classloader to work... anyone else able to use it?
  • 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

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

  • 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

0 comments:

Post a Comment