2

I try to run the following:

import javax.script.*;

public class Main {

    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");

        System.out.println(engine.eval("(1..10).sum()"));
  }
}

which I extracted from http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy

but I get a NullPointerException at the last line. I have Java 7u25 installed. I also have Groovy 2.2.1 installed. I am running this from Eclipse Kepler.

A similar javax script test with "Javascript" worked fine. Is there some step I am missing to get this working with groovy?

Thanks, Tara

greenTara
  • 53
  • 5

1 Answers1

2

Make sure the Groovy jars are on your classpath before the above code.

Running:

java -cp .:~/.gvm/groovy/2.2.1/lib/* Main

Shows the error you describe, but running:

java -cp ~/.gvm/groovy/2.2.1/lib/*:. Main

Shows the output:

55
tim_yates
  • 154,107
  • 23
  • 313
  • 320
  • This works when I navigate to the bin directory under my project and then use the command line. But I normally use the Eclipse GUI. Is there a straight-forward way to do this in Eclipse? I am trying to use the Classpath tab in the Run configuration, and I am able to enter individual jars and/or the folder they are contained in. I moved these up so that they occur before the source folder. I tried various combinations, but I am still getting the same error. (I am using the default package.) – greenTara Jan 20 '14 at 22:59
  • Never mind, I got it working through the Build Path. – greenTara Jan 20 '14 at 23:09