我正在使用Java电机来运行Groovy Scripts,我想加载运行Groovy脚本所需的所有依赖项。目前,我正在装载罐子如下:
File file = new File(elem);
URL url = file.toURI().toURL();
URL[] urls = new URL[] { url };
@SuppressWarnings("resource")
ClassLoader cl = new URLClassLoader(urls);
try {
cl.loadClass("com.example.ScriptLibrary");
return cl;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new MalformedURLException("[Error] : Bad library path\n");
}
它可以工作,所以我的代码加载了jar库。我想知道Groovy依赖项是否还有其他方法,或者我可以像我一样加载Java代码。