如何在Java中运行Groovy [英] How to run groovy in Java

查看:46
本文介绍了如何在Java中运行Groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我尝试过各种不同的方法来在Java中运行groovy并没有运气,已经阅读了一些文档,但目前还不清楚.

Hi everyone tried different ways to run a groovy in java with no luck, had read some documentation but things aren't that clear at the moment.

任何人都可能知道如何运行这种常规操作吗?

Anyone may know how to run this groovy?

package com.test.dev.search;

public class SearchQueryBase implements SearchQuery {    

    public QueryString getMatterQuery( SearchFilter filter ) {
        String[] terms = filter.getSearchTerm().toLowerCase().split( " " );
        ...
        ...
        ...
    }
}

这是一个.groovy文件(上面的文件),我尝试了以下操作来运气不佳.

This is a .groovy file (the one from above), I've tried the follow to run it without luck.

下面是我要在其中运行上述Groovy并执行 getMatterQuery()的Java类,以查看java main的输出.

Down here is the Java class in which I want to run the above Groovy and execute getMatterQuery() to see the output from java main.

public static void main(String args[]) throws CGException {

    String TEMPLATE_PACKAGE_PREFIX = "<path_to_groovy_file.";

    String templateFileName = TEMPLATE_PACKAGE_PREFIX + "SearchQueryBase";

    SearchFilter test = null;

    Binding binding = new Binding();
    binding.setVariable("filter", test);

    GroovyShell shell = new GroovyShell(binding);

    shell.evaluate(templateFileName);

    System.out.println("Finish");
}

编辑#1

这是我运行它时遇到的错误;

This is the error I'm getting when I run it;

Exception in thread "main" groovy.lang.MissingPropertyException: No such property: Common for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)

推荐答案

1.

GroovyShell.evaluate(java.lang.String scriptText)接受字符串作为常规文本(内容),而您尝试使用文件名来代替它.使用 shell.evaluate(new File(templateFileName))

the GroovyShell.evaluate(java.lang.String scriptText) accepts string as a groovy text (content), and you try to call it with filename instead. use shell.evaluate( new File(templateFileName) )

2.

您可以继续使用 shell.evaluate(new File(...)),但仅将 getMatterQuery()方法的内容保留在常规文件中:

you can continue using shell.evaluate( new File(...) ) but keep in your groovy file only content of the method getMatterQuery():

String[] terms = filter.getSearchTerm().toLowerCase().split( " " );
...
...
...

所以您将拥有普通的脚本,并且您的代码应该可以工作

so you'll have groovy script, and your code should work

3.

如果要保持常规,并使用参数从此类调用方法 getMatterQuery(),则您的Java代码应如下所示:

if you want to keep groovy as a class and call the method getMatterQuery() from this class with parameter, then your java code should be like this:

import groovy.lang.*;
...

public static void main(String[]s)throws Exception{
    GroovyClassLoader cl=new GroovyClassLoader();
    //path to base folder where groovy classes located
    cl.addClasspath(path_to_groovy_root); 
    //the groovy file with SearchQueryBase.groovy
    //must be located in "com/test/dev/search" subfolder under path_to_groovy_root
    Class c = cl.loadClass("com.test.dev.search.SearchQueryBase");
    SearchQuery o = (SearchQuery) c.newInstance();
    System.out.println( o.getMatterQuery(test) );
}

这篇关于如何在Java中运行Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆