No suitable classloader found for grab

前端 未结 7 1406
梦毁少年i
梦毁少年i 2020-12-06 10:13

I have this at the beginning of a class:

@Grab(group = \'org.ccil.cowan.tagsoup\', module = \'tagsoup\', version = \'1.2\')
class MyClass{...
7条回答
  •  执念已碎
    2020-12-06 10:18

    The Problem

    Looking at the source code, this exception is thrown whenever the supplied ClassLoader's name (or it's superclasses) is not groovy.lang.GroovyClassLoader or org.codehaus.groovy.tools.RootLoader. i.e. The target classloader must be an instance of the aforementioned classes (a bit restrictive IMHO).

    A Solution

    Currently I don't know how to configure a specific classloader using @Grape/@Grab/@GrabConfig annotations. The closest would be to use @GrabConfig(systemClassLoader=true), and ensure the System classloader is an instance of one of the above ClassLoader classes.

    If anyone does know, please let me know (and I'll update this answer).

    A Workaround

    The following code will programmatically download your Grapes, and load them into the supplied GroovyClassLoader (admittedly, not quite what you want).

    def loadGrapes(){
        ClassLoader classLoader = new groovy.lang.GroovyClassLoader()
        Map[] grapez = [[group : 'org.ccil.cowan.tagsoup', module : 'tagsoup', version : '1.2']]
        Grape.grab(classLoader: classLoader, grapez)
        println "Class: " + classLoader.loadClass('org.ccil.cowan.tagsoup.jaxp.SAXParserImpl')
    }
    

提交回复
热议问题