背景(スキップすることができます):基本的なGroovyスクリプトエンジンのセットアップ
は、私は最近、Javaで軽量サーバーを書いた新しいポートで接続し、クライアントが接続しているとき、それはソケットまで彼らに自分のスレッドを与えるためのポーリング閉じます。さて、クライアントが接続されたら、何をする必要があるのかは、XMLファイル要求を特別な方法で準備することです。クライアントがfile1.xmlを要求した場合、サーバーはfile1.xmlを読み込み、JSONに解析してjsonオブジェクトをクライアントに送信する必要があります。
問題特定(今読んで始めてください): JavaのJSONオブジェクトにXMLファイルを解析する必要があります。私はこの仕事のためにGROOVYに勧められました。私のMacとubuntuの両方のパーティションにインストールするのは簡単でしたが、インライングルーヴィーの作業をすることはできません。その理由はほとんどありません。
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
フォルダにあります。私がコンパイルしようとすると:
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
私は、コンパイル段階で何かを間違ってやってる感じ:
javac test.java
私は6つのエラーが発生します。このコンパイルと実行をどうすればできますか?非常に多くのtest.java
として
は、ScriptEngineのは、本当にすべてのスクリプトの依存関係を搭載したエンジンであり、プロジェクトのために? – djangofan