1

I am using Groovysh and I need to load some classes with Grape. Right now I am trying this:

> @Grab(group='group.example', module='module.example', version='1.2.3')
> import group.example.TheClass
> theClass = new TheClass()

However, after I enter the import, Groovy says

groovysh_evaluate: 4: unable to resolve class

The curious thing is if I try this:

> @Grab(group='group.example', module='module.example', version='1.2.3')
> import group.example.TheClass
> theClass = new TheClass()
> import group.example.TheClass
> theClass = new TheClass()

The first import and declaration fail, but the second ones succeed. This might be a bug or maybe I'm doing something wrong. I'm currently using Groovy 2.4.12, any help would be greatly appreciated. When I try the same thing in a script, it works fine so I am very confused. I've also tried playing with interpreterMode but also with no luck.

M. Justin
  • 14,487
  • 7
  • 91
  • 130

2 Answers2

1

I found that if instead of

import group.example.TheClass

I do

import group.example.*

It works. Still seems like this is a bug, but here is a solution in case anyone else comes across this issue.

  • similar issues occur in Jenkins scripting. Code "GroovyShell shell = new GroovyShell() def common_code = shell.parse(new File(env.WORKSPACE + '/utils/common_functions.groovy'))" gives an class not found error. I've changed the code to "org.jenkinsci.plugins.pipeline.modeldefinition.*" to make it work. Thanks for the suggestion @ScienceSage – Rajesh Kazhankodath Sep 04 '22 at 02:44
0

Try this way:

groovy:000> import groovy.grape.Grape
===> [import groovy.grape.Grape]
groovy:000> Grape.grab(group:'net.sourceforge.htmlunit', module:'htmlunit', version:'2.44.0')
===> null
groovy:000> import com.gargoylesoftware.htmlunit.WebClient
===> [import groovy.grape.Grape, import com.gargoylesoftware.htmlunit.WebClient]
Tomislav Nakic-Alfirevic
  • 10,017
  • 5
  • 38
  • 51