3

I just started learning about groovy and trying to transpose my java code to groovy scripts. Usually java allows you have a class with only methods that you can call from other classes. I wanted to translate that to groovy. I have in one file - lets call it File1- a method like this:

def retrieveData(String name){  
// do something
}

and in the second file, File2, I call File1 like this:

def file1Class = this.class.classLoader.parseClass(new File("../File1.groovy"))

and then try to call the method in File1 like this:

def data = file1Class.retrieveData("String")

but it keeps giving me this error - MissingMethodException:

groovy.lang.MissingMethodException: No signature of method: static File1.retrieveData() is applicable for argument types: (java.lang.String) values: [String] Possible solutions: retrieveData(java.lang.String)

so it does recognize that I am sending in the correct number of parameters and even the correct object, but it isn't running the method as it should? Is there something I am missing? I tried to remove the object definition from the method - in other words - like this:

def retrieveData(name){  
// do something
}

but that didn't work either. I am clueless about what the next step would be. Can anyone please help push me in the right direction? I would greatly appreciate it.

droidnoob
  • 333
  • 5
  • 17

1 Answers1

0

See the answer provided in this StackOverflow reponse.

Use the GroovyScriptEngine class. What does the GroovyScriptEngine do? From the docs:

Specific script engine able to reload modified scripts as well as dealing properly with dependent scripts.

See the example below.

def script = new GroovyScriptEngine( '.' ).with {
  loadScriptByName( '..\File1.groovy' )
} 
this.metaClass.mixin script

retrieveData()

Note how we use the loadScriptByNamemethod to

Get the class of the scriptName in question, so that you can instantiate Groovy objects with caching and reloading.

This will allow you to access Groovy objects from files however you please.

Community
  • 1
  • 1
Nathan
  • 3,082
  • 1
  • 27
  • 42
  • I tried that but it completely ignored the classpath. I put in necessary jar files into the ext folder so that the soapUI can have access to it, and it does using the method I referenced in the question, but when I do it that way, it throws a MultipleCompilationErrorsException: unable to resolve class HSSFCell – droidnoob Feb 10 '17 at 16:57
  • Are you trying to call a directory? If so, [check out this answer](http://stackoverflow.com/questions/15565961/groovyscriptengine-load-groovy-scripts-from-subfolder#15566768) which describes loading from a subfolder. Also, if you are calling resources that are defined by a relative path, extra care will need to be taken in switching between groovy classes. – Nathan Feb 10 '17 at 17:14
  • The File1.groovy and the xml holding the File2.groovy are both in the Documents folder. Yes, I had to edit the loadScript by Name to this:`loadScriptByName( '../../../../Users/myname/Documents/File1.groovy' )` but the jar files should go to the SOAPUI_HOME/bin/ext folder. So why won't it see it? while reading File1.groovy? – droidnoob Feb 10 '17 at 17:18
  • What did you change it too? – Nathan Feb 10 '17 at 17:27
  • I changed the directory it was pointing to, so it points to the correct location of the File1.groovy. I even tried moving the File1.groovy file into the folder containing the jars but still nothing – droidnoob Feb 10 '17 at 17:28
  • Can you place all the files in the same directory for testing purposes? If you are going to be calling the Users home directory try something similar to `File user_home = new File("${System.getProperty('user.home')}$fileName".toString())` – Nathan Feb 10 '17 at 17:34
  • still having the same issues. I moved the xml file containing the File2.groovy and the File1.groovy file to the folder containing the jar files in the SOAP_HOME/bin/ext folder, and its still giving the same error – droidnoob Feb 10 '17 at 17:48