How do I properly call GroovyScriptEngine?

advertisements

I'm testing Groovy but I can't figure out how to properly call GroovyScriptEngine. It keeps producing an error below.

org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack

Song.Groovy

class Song {

 def args;
{
  println "Song has been called." + args;
 }

 String getArtist(){
    return "sdfsdf";
 }

 public String toString(){
    return "Hey!";
 }

 }

Java Main ->

String[] paths = { "C:\\Users\\User\\workspace\\GroovyTest\\src\\groovy" };

        GroovyScriptEngine gse = new GroovyScriptEngine(paths);
        Binding binding = new Binding();
        Object s = "Default...";

        binding.setVariable("args", s);

        gse.run("Song.groovy", binding);

the args variable also produce null..

What to do ?


You are loading a class!

If you want to test your class, try something like this in the end of your Song.groovy:

// Instantiate an object of your class and use some methods!
def song = new Song()
println song.getArtist();

When you run

gse.run("Song.groovy", binding);

You are basically loading your class, but you are not doing anything with it.

See this example here