You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Jar包中加载并执行Groovy脚本?

问题:从Runtime Jar的类路径中加载并执行Groovy脚本

我使用Groovy执行一组Groovy脚本,当runtime jar与Web应用一同部署、脚本位于C:\jboss-eap-7.4\bin(Java工作目录)时运行正常。出于可移植性限制,需要将这些脚本移入runtime jar,从类路径加载执行,求协助实现Web应用中runtime jar内Groovy脚本的运行。

Jar包内的plugins目录结构


当前从文件系统执行脚本的实现(经GPT-3优化后代码)

final String PLUGIN_DESCRIPTOR = "Plugins.groovy"
final class PluginBootStrapper 
{
    private GroovyScriptEngine scriptEngine = null;
    private GroovyShell shell;
    private GroovyClassLoader classLoader = null;
    private final boolean loadFromClasspath;

    private final List<Plugin> allPlugins = null;
    
    private static final Logger logger = LogManager.getLogger(PluginBootStrapper.class);
    
    public PluginBootStrapper() 
    {
        System.out.println("Inside PluginBootStrapper")
        logger.info("Inside PluginBootStrapper")
        String pluginsDir = System.getProperty(CSMProperties.endorsed_plugins_dir)//".\\plugins"//System.getProperty(CSMProperties.endorsed_plugins_dir)
        loadFromClasspath = true
        shell =  new GroovyShell();
        //scriptEngine = new GroovyScriptEngine(CommonUtils.getAllDirectories(pluginsDir))

        logger.info "Plugins Directory:"+pluginsDir
        println "Plugins Directory:"+pluginsDir
        
        allPlugins = loadDescriptor().invokeMethod("getAllPlugins", null)       
    }
    
    private Object loadDescriptor()
    {
        Object pluginDescriptor = bootStrapScript(CSMProperties.get(CSMProperties.PLUGIN_DESCRIPTOR))                                                                 
        pluginDescriptor                          
    }
        
    Object bootStrapScript(String script)
    {
        String pluginsDir = System.getProperty(CSMProperties.endorsed_plugins_dir)
        if (pluginsDir != null) {
            script = pluginsDir + script
        }
        
        printClassPath(this.class.getClassLoader())

        Object pluginScript = null
        //logger.info "script: "+ script
        //String path = this.getClass().getClassLoader().getResource(script).toExternalForm()
        //logger.info "bootStrapScript script "+ script
        logger.info "bootStrapScript script: "+ script + ", path: "+ new File(script).absolutePath
        println "bootStrapScript script: "+ script + ", path: "+ new File(script).absolutePath
        if (this.loadFromClasspath) {
            pluginScript = new GroovyShell(this.class.getClassLoader()).evaluate(new File(script));     //<-- Line no:60
            /* classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
            pluginScript = classLoader.parseClass(new File(script)); */
            return pluginScript
        } else {
            pluginScript = scriptEngine.loadScriptByName(script).newInstance()
            return pluginScript
        }
        
        return pluginScript
    }
    
    public List<Plugin> getAllPlugins()
    {
        return allPlugins
    }

    def printClassPath(classLoader) {
        println "$classLoader"
        classLoader.getURLs().each {url->
            println "- ${url.toString()}"
        }
        if (classLoader.parent) {
            printClassPath(classLoader.parent)
        }
    }
    
}

CommonUtils.getAllDirectories方法

public static String[] getAllDirectories(String directory)
{
    logger.info("Inside CommonUtils"+directory)
    def dir = new File(directory)
    def dirListing = []
    if (dir.exists()) {
        logger.info "Looking for plugins in "+dir.absolutePath+" directory."
        
        dir.eachFileRecurse {file->
             if(file.isDirectory()) {
                 dirListing << file.getPath()
                 logger.info "Using "+file.getPath()+" plugin folder."
             } else {
                 logger.info "Using "+file.getPath()+" plugin file."
             }
        }
    } else {
        logger.error directory+" folder does not exist. Please provide the plugin files."
    }
    
    dirListing.toArray() as String []
}

测试运行命令

java -cp runtime-2.0-jar-with-dependencies.jar;.runtime-2.0-jar-with-dependencies.jar; -Dendorsed_plugins_dir=runtime-2.0-jar-with-dependencies.jar\\ com.Main %*

运行输出截图

运行错误输出


此前遇到的问题

目前使用上述代码,若将包含Groovy脚本的plugins文件夹放入Jar包,会提示无法在C:\jboss-eap-7.4\bin目录下找到Plugins.groovy。

War包内Jar包的结构截图:
War包内runtime.jar的结构


内容的提问来源于stack exchange,提问作者Ricky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:05:29