java脚本引擎groovy_java - Groovy脚本引擎基本设置 - 堆栈内存溢出

背景(可以跳过):

我最近用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

我觉得我在编译阶段做错了。 我如何获得此编译和运行?

非常感谢帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值