2017-08-31 49 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\\"