开发者

Basic Groovy Script Engine Setup

Background (can skip):

I recently wrote a lightweight server in Java that polls for new connections on a port, and then when a client connects it gives them their own thread until the socket closes. Now, what I need to do once a client is connected is prepare for XML file requests in a special way; If a client requests file1.xml, the server needs to read in file1.xml, parse it to JSON, and send the json object to the client.

Problem Specific (now start reading): I need to parse XML files into JSON objects in Java. I was recommended GROOVY for this task. It was a breeze to install on both my mac and ubuntu partitions, but I cannot get inline groovy wo开发者_StackOverflow中文版rking, and the reason is most likely very trivial. Here's what I'm testing now (at this point, I'm just trying to get embedded groovy working):

test.groovy

output = "Hello ${input}!" 

test.java

import groovy.lang.Binding; 
import groovy.util.GroovyScriptEngine; 

String[] roots = new String[] { "/home/nick/Documents" }; 
GroovyScriptEngine gse = new GroovyScriptEngine(roots); 
Binding binding = new Binding(); 
binding.setVariable("input", "world"); 
gse.run("test.groovy", binding); 
System.out.println(binding.getVariable("output")); 

Both of these files are in my /home/nick/Documents folder. When I try to compile with:

javac test.java 

I get 6 errors:

test.java:4: class, interface, or enum expected 
String[] roots = new String[] { "/home/nick/Documents" }; 
^ 
test.java:5: class, interface, or enum expected 
GroovyScriptEngine gse = new GroovyScriptEngine(roots); 
^ 
test.java:6: class, interface, or enum expected 
Binding binding = new Binding(); 
^ 
test.java:7: class, interface, or enum expected 
binding.setVariable("input", "world"); 
^ 
test.java:8: class, interface, or enum expected 
gse.run("test.groovy", binding); 
^ 
test.java:9: class, interface, or enum expected 
System.out.println(binding.getVariable("output")); 
^ 
6 errors 

I feel I'm doing something wrong at the compile stage. How can I get this compiling and running?

Help very much appreciated


As test.java is a Java class, and not a Groovy script, you'll need to wrap the code in a class (renamed to Test.java with a capital T). You also need to catch or throw few Exceptions:

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException ;
import groovy.util.ScriptException ;
import java.io.IOException ;

public class Test {
  public static void main( String[] args ) throws IOException, ResourceException, ScriptException {
    String[] roots = new String[] { "." };
    GroovyScriptEngine gse = new GroovyScriptEngine(roots);
    Binding binding = new Binding();
    binding.setVariable("input", "world");
    gse.run("test.groovy", binding);
    System.out.println(binding.getVariable("output"));
  }
}

Then, you'll need to compile this Java class with groovy on the classpath (using the wildcard path requires java 6, otherwise you'll need to fill the complete path to the groovy-all-*.jar):

javac -cp $GROOVY_HOME/embeddable/*:. Test.java

And run it with the correct classpath too:

java -cp $GROOVY_HOME/embeddable/*:. Test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜