背景(可以跳过):
我最近用Java编写了一个轻量级服务器,该服务器轮询端口上的新连接,然后当客户端连接时,它将为它们提供自己的线程,直到套接字关闭。 现在,一旦连接客户端,我需要做的就是以一种特殊的方式准备XML文件请求。 如果客户端请求file1.xml,则服务器需要读入file1.xml,将其解析为JSON,然后将json对象发送给客户端。
特定问题(现在开始阅读):我需要将XML文件解析为Java中的JSON对象。 建议我将GROOVY用于此任务。 在我的mac和ubuntu分区上都安装起来很容易,但是我无法进行内嵌的常规工作,原因很可能很简单。 这是我现在正在测试的内容(目前,我只是想使嵌入式groovy正常工作):
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"));
这两个文件都在我的/home/nick/Documents文件夹中。 当我尝试编译时:
javac test.java
我收到6个错误:
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
我觉得我在编译阶段做错了。 我如何获得此编译和运行?
非常感谢帮助