2017-08-31 66 views
0

我遍历目录载入我的jar文件Groovy的装载jar文件

import groovy.io.FileType 

def list = [] 

def dir = new File("C:\\Whatever\\") 
dir.eachFileRecurse (FileType.FILES) { 
    if(it.name.endsWith('.jar')) { 
     println it 
     this.getClass().classLoader.rootLoader.addURL(new File(it).toURI().toURL()) 
    } 
} 

而且我得到以下错误:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(java.io.File) 

    at ConsoleScript21$_run_closure1.doCall(ConsoleScript21:9) 

    at ConsoleScript21.run(ConsoleScript21:6) 

我在做什么错?为什么我不能像这样加载我的jar文件?

+1

最有可能抱怨你使用'新文件中的错误(它)'而'它''已经是'File' – Aaron

+0

除非闭包是单线,否则我总是喜欢输入参数类型(如果适用)和名称,而不是使用“it”。 – Steinar

回答

2
this.getClass().classLoader.rootLoader.addURL(new File(it).toURI().toURL()) 

应该成为

this.getClass().classLoader.rootLoader.addURL(it.toURI().toURL()) 

dir.eachFileRecurse (FileType.FILES)遍历文件系统子树下文件对象的集合开始"C:\\Whatever\\"