我创建了两个 groovy 脚本,如下所示。一个脚本有一个在另一个脚本中实例化的类。两者都在默认包中。
当我尝试运行时ImportGpsData.groovy
,出现以下异常...
Caught: java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at ImportGpsData$_run_closure1.doCall(ImportGpsData.groovy:10)
at ImportGpsData.run(ImportGpsData.groovy:6)
Caused by: java.lang.RuntimeException: No suitable ClassLoader found for grab
at DateParser.<clinit>(DateParser.groovy)
... 2 more
导入GpsData.groovy
def file = new File('fells_loop.gpx')
def slurper = new XmlSlurper()
def gpx = slurper.parse(file)
gpx.rte.rtept.each {
println it.@lat
println it.@lon
def parser = new DateParser()
println parser.parse(it.time.toString())
}
日期解析器.groovy
@Grapes(
@Grab(group='joda-time', module='joda-time', version='2.3')
)
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
class DateParser {
def String parse(time){
def printableTime = new DateTime(time)
def format = DateTimeFormat.forPattern('MM/dd/yyyy - hh:mm aa')
return printableTime.toString(format)
}
}
我发现了其他一些处理No Suitable classloader found for grab
错误的 StackOverFlow 问题。一个答案建议@GrabConfig(systemClassLoader=true)
在@Grapes 语句中使用,但是添加它会导致编译错误,我在第二行收到错误意外标记@。
@Grapes([
@Grab(group='joda-time', module='joda-time', version='2.3')
@GrabConfig( systemClassLoader=true )
])
上面的使用方式给出了第 3 行中发现的意外标记 @...在 @GrabConfig 之前添加逗号会给出以下错误
Multiple markers at this line
- Groovy:Invalid duplicate class definition of class DateParser : The source F:\GroovyEclipses\src\DateParser.groovy contains at least two definitions of the class DateParser.
- General error during conversion: No suitable ClassLoader found for grab java.lang.RuntimeException: No suitable ClassLoader found for grab
@Grapes
经过进一步分析,我发现我@Grab
在使用任何脚本时都会遇到此错误。但是我必须使用它们来处理joda-time
.