Dr3ko
Dr3ko

Reputation: 313

How to run groovy in Java

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( " " );
        ...
        ...
        ...
    }
}

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

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");
}

EDIT #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)

Upvotes: 0

Views: 3374

Answers (1)

daggett
daggett

Reputation: 28564

1.

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.

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.

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) );
}

Upvotes: 1

Related Questions