'No suitable ClassLoader found for grab while Instantiating a class

I have created two groovy scripts as below. One script has a class which is instantiated in the other script. Both are in default package.

When I'm trying to run ImportGpsData.groovy I'm getting the following exception...

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

ImportGpsData.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())
}

Dateparser.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)
    }
}

I've found some other StackOverFlow questions that dealt with No Suitable classloader found for grab error. One answer suggested using @GrabConfig(systemClassLoader=true) inside @Grapes statement however adding it is resulting in compilation error, I'm getting error unexpected token @ in line two.

@Grapes([
    @Grab(group='joda-time', module='joda-time', version='2.3')
    @GrabConfig( systemClassLoader=true )
])

Above way of using it gave unexpected token @ found in line 3... Adding a comma before @GrabConfig is giving the below error

 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 

After further analysis, I have figured that I'm getting this error when ever I user @Grapes and @Grab in any of my scripts. However I have to use them to work with joda-time.



Solution 1:[1]

Not sure if you were able to resolve this, if not then try to compile the class file first:

groovyc Dateparser.groovy

and then do

groovy ImportGpsData.groovy

should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Subodh Brahmi