How do I auto load a database jar in Groovy without using the -cp switch?

不羁的心 提交于 2019-11-27 12:29:28

Summarized from Groovy Recipes, by Scott Davis, Automatically Including JARs in the ./groovy/lib Directory:

  1. Create .groovy/lib in your login directory
  2. Uncomment the following line in ${GROOVY_HOME}/conf/groovy-starter.conf

    load !{user.home}/.groovy/lib/*.jar

  3. Copy the jars you want included to .groovy/lib

It appears that for Groovy 1.5 or later you get this by default (no need to edit the conf), just drop the jars in the /lib dir.

Joey Gibson

There are a few ways to do it. You can add the jar to your system's CLASSPATH variable. You can create a directory called .groovy/lib in your home directory and put the jar in there. It will be automatically added to your classpath at runtime. Or, you can do it in code:

this.class.classLoader.rootLoader.addURL(new URL("file:///path to file"))

groovy is just a wrapper script for the Groovy JAR that sets up the Java classpath. You could modify that script to add the path to your own JAR, as well, I suppose.

You could add the following shebang to the first line of your Groovy script:

#!/usr/bin/env groovy -cp ojdbc5.jar

Then, mark the script executable:

chmod u+x RunScript.groovy

Now, running the script by itself will set the classpath automatically.

./RunScript.groovy
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!