示例如下:
1) pom.xml依赖
- <dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy</artifactId>
- <version>2.1.6</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy-jsr223</artifactId>
- <version>2.1.6</version>
- </dependency>
- static void simpleTest() throws IOException, InstantiationException, IllegalAccessException, ResourceException, ScriptException{
- String[] roots = new String[] { "src/main/groovy/com/mobile263/billing/groovy/" };
- //通过指定的roots来初始化GroovyScriptEngine
- GroovyScriptEngine gse = new GroovyScriptEngine(roots);
- GroovyObject groovyObject = (GroovyObject) gse.loadScriptByName("TestScript.groovy").newInstance();
- String result = (String) groovyObject.invokeMethod("output", "hello");
- System.out.println(result);
- }