0

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Buddha
  • 4,339
  • 2
  • 27
  • 51
  • Which version of Java? My guess would be OpenJdk v1.7? – tim_yates Feb 09 '14 at 13:59
  • Nope, I'm using Oracle JDK v1.7 never installed anything other than that. – Buddha Feb 09 '14 at 14:48
  • Interesting. Which build? Which is? – tim_yates Feb 09 '14 at 17:32
  • Java is build 45. Groovy version is 2.2.1. – Buddha Feb 09 '14 at 18:00
  • When I use Grab without classes, I could use joda-time in a script, but when I'm using Grab and when the classes are involved this issue is coming. – Buddha Feb 09 '14 at 18:03
  • What's the exact command you use to execute the script? This error occurs if the GroovyClassLoader or RootClassLoader can't be found as direct or indirect CL parent. – Andre Steingress Feb 09 '14 at 20:42
  • I've executed as a Groovy Script from eclipse... – Buddha Feb 10 '14 at 03:41
  • Does it work by simply executing `groovy ImportGpsData.groovy` after cding into the source directory? – Andre Steingress Feb 10 '14 at 10:34
  • Nope, I get error saying that class DateParser is not found. unable to resolve class DateParser. However I can see that the class file is being generated when I use groovyc on DateParse.groovy. – Buddha Feb 10 '14 at 11:09
  • The initial code works fine for me (I remove the `@Grapes` bit and just use the `@Grab`)... Is this part of a bigger system? Maybe time to move to use a build tool like gradle? – tim_yates Feb 10 '14 at 11:28
  • That's weird. I tested this locally and it worked. The `groovy` commands adds "." to the class path automatically. – Andre Steingress Feb 10 '14 at 18:59
  • @tim_yates nope, this is not part of any bigger system. I'm just learning groovy and the above is one that I tried following some example. Not sure if something is wrong with my installation. – Buddha Feb 11 '14 at 05:27
  • possible duplicate of [No suitable classloader found for grab](http://stackoverflow.com/questions/4611230/no-suitable-classloader-found-for-grab) – Nick Grealy Oct 31 '14 at 03:17

1 Answers1

0

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.