简体   繁体   中英

Can I load jar dynamically in groovy?

I have one JAVA program to run groovy script.

Example:
String returnMessage = new GroovyScriptEngine(".").run("hello.groovy", binding);

I want to use jar which added at runtime by the user from my application(eg jdbc.jar), so that users do not need to know where it is. How can I load jar? so that I can use classes defined under this jar only into the hello.groovy .

hello.groovy contains:
println "Hello Groovy!" // code of access the class of jdbc.jar SomeClass someObj = new SomeClass();

You can load a jar at runtime by using @Grapes dependency management meta-data. Add a grapes block to the top of your hello.groovy like shown below. The dependency will be downloaded from maven at run time. The block below is using Oracles odbc jdbc library, but you should be able to find your own on Maven Central. Search for your library on Maven Central and there will be a 'Grapes' tab for that version where you can copy a block similar to this:

@Grapes(
    @Grab(group='com.oracle', module='ojdbc14', version='10.2.0.4.0')
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM