Hello World example with javax on groovy script gives null pointer

2.1k views Asked by At

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

1

There are 1 answers

2
tim_yates On BEST ANSWER

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