문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top