Java の JSR-223 実装を試す

# 概要
皆さんは [JSR-223](https://www.jcp.org/en/jsr/detail?id=223) をご存知でしょうか？Java で Script 言語のコードを動かす実装に関する仕様で、それだけ聞くとなかなか夢を広げるアイデアです。例えば、 Java の標準には JavaScript を動かすための ScriptEngine が含まれており、それを使えば Java と既存の JavaScript コードを移植作業なしに連携させることが可能です。興味深い多言語のライブラリがあるとき、まずはこの ScriptEngine を使って連携を確かめ、有用であると判断できたら移植する、という使い方にも適します。

実装されていた JSR-223 実装の一覧は https://java.net/projects/scripting/sources/svn/show/trunk/engines/java?rev=236 で見ることができます。なお、最終更新から10年近く経ったものもあり、置き去り感がたまらないです。

## Java
この JSR-223 の中には Java に対するものもあったようです。残念ながら標準に組み込まれることはなく、今も [svn で眠っている](https://java.net/projects/scripting/sources/svn/show/trunk/engines/java?rev=236)ようです。Java のソースコードを与えて実行できるというと、Java9から登場する JShell を思い出します。Early Access をダウンロードすれば今すぐ使えるのに、今更できることの限られている Java の JSR-223 を引っ張り出す意味がどこまであるのかは不明ですが、暇つぶしになればと思ってやってみました。

# ビルド
ソースコードをダウンロードしてビルドするのが最も楽でしょう。今回は Gradle を使ってjarを作り、それをビルドパスに含めて使いました。

# 使う
Java8 の動く環境であれば Lambda も Stream API も使えます。

```java:Script.java
package com.sun.script.java;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class Script {
    public static void main(final String[] args) {
        final String script = "import java.util.*;"
                + "class Hello {"
                + "public static void main(final String[] args) {"
                + "StringBuilder sb = new StringBuilder();"
                + "Arrays.asList(\"h\", \"e\", \"l\", \"l\", \"o\").forEach(c -> {sb.append(c);});"
                + "System.out.println(sb.toString());}}";
        final ScriptEngine engine = new JavaScriptEngineFactory().getScriptEngine();
        try {
            final java.lang.Object run = engine.eval(script);
        } catch (final ScriptException e) {
            e.printStackTrace();
        }

    }
}
```

仕方ないことではありますが、「JavaScriptEngineFactory」 という名前だと Java のものなのか JavaScriot のものなのかわかりにくいです。今回のは Java の ScriptEngineFactory です。
Script として渡すコードの Main Class を public で宣言すると、ファイル名と Main クラスの名前が一致しないと言われコンパイルエラーとなります。

```:実行結果
hello
```

# 何がうれしいのか？
プラグイン的な仕組みを実装したり、Java 学習用のアプリケーションを開発するのに利用したり、簡単な検証用のコードを動かすのに役立ちそうです。おや、JShell の用途と大分重なっていますね……

# 結論
1. Java の軽い検証には JShell を使いましょう([Open JDK9 Early Access](https://jdk9.java.net/download/))
2. Java の文法で Script を書きたいなら [Groovy](http://www.groovy-lang.org/) を使うとよいでしょう
3. Java 以外の言語で記述されたライブラリと Java の連携を急ぎで確かめたい場合には有用です

# 参考
1. [Original Project](https://java.net/projects/scripting/sources/svn/show/trunk/engines/java?rev=236)
2. [今回のソース全体(GitHub repository)](https://github.com/toastkidjp/java-engine)

## 手前味噌
以前、JavaFX と ScriptEngine を組み合わせたアプリケーションを開発したことがあります。
1. [JavaFX と ScriptEngine を組み合わせた学習用アプリケーションを作る](http://qiita.com/y_q1m/items/5bd4af479cfae293100d)
2. [上記アプリケーションの repository](https://github.com/toastkidjp/javafx_advent2015)……現在は Jython/JavaScript/Groovy/Clojure に対応しています。

---
# おまけ
リポジトリにあった他の JSR-223 にどんなものがあったかをまとめてみました。まったく知らない言語がたくさんあって世の中の広さと歴史の重みを感じます。

| jsr-223 project | comment | license | latest author | revision | latest modified | log entry |
|:---|:---|:---|:---|:---|:---|:---|
| [beanshell](https://java.net/projects/scripting/sources/svn/show/trunk/engines/beanshell?rev=236) | JavaVM上で動作するスクリプト言語 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/beanshell/LICENSE.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [browserjs](https://java.net/projects/scripting/sources/svn/show/trunk/engines/browserjs?rev=236) | native JavaScript engine embedded in web browsers | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/browserjs/LICENSE.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [ejs](https://java.net/projects/scripting/sources/svn/show/trunk/engines/ejs?rev=236) |  Embedded JavaScript language | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/ejs/LICENSE.TXT?rev=236) | sundararajana | 88 | over 9 years ago | Fixed license text in all LICEN...
| [freemarker](https://java.net/projects/scripting/sources/svn/show/trunk/engines/freemarker?rev=236) |  Freemarker general purpose template engine in Java | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/freemarker/LICENSE.TXT?rev=236) / [LGPL](https://java.net/projects/scripting/sources/svn/content/trunk/engines/freemarker/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 192 | over 8 years ago | * Updated README.TXT files to re...
| [groovy](https://java.net/projects/scripting/sources/svn/show/trunk/engines/groovy?rev=236) | Groovy | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/groovy/LICENSE.TXT?rev=236) / [BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/groovy/THIRDPARTYLICENSES.TXT?rev=236) | ceefour | 228 | almost 8 years ago | Added Bundle-Version manifest to...
| [jacl](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jacl?rev=236) |  Tcl (Tool Command Language)のJava実装 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jacl/LICENSE.TXT?rev=236) / [BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jacl/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 155 | about 9 years ago | Added a note on accessing/updat...
| [jaskell](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | Haskell の Java 実装、これ以上新しいコードは行方不明 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jaskell/LICENSE.TXT?rev=236) [BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jaskell/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [java](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | Java | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/java?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [javascript](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | JavaScriot | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/javascript?rev=236) / [MPL](https://java.net/projects/scripting/sources/svn/content/trunk/engines/javascript/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 192 | over 8 years ago | * Updated README.TXT files to re...
| [jawk](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | AWK の Java 実装 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jawk?rev=236) / [GPL](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jawk/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [jelly](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  ??? | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jelly?rev=236) / [Apache License Version 2.0](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jelly/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 136 | over 9 years ago | * Made makefile changes to buil...
| [jep](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  Java Math Expression Parser | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jep?rev=236) / [GPL](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jep/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [jexl](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  ??? | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jexl?rev=236) / [Apache License Version 2.0](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jexl/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [jruby](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | Ruby の Java 実装 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jruby?rev=236) / [under a tri CPL/GPL/LGPL license](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jruby/THIRDPARTYLICENSES.TXT?rev=236) |  yokolet | 234 | over 7 years ago | updating JRuby engine samples
| [jst](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  JavaScript templates | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jst?rev=236) / [Apache License, Version 2.0](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jst/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 88 | over 9 years ago | Fixed license text in all LICEN...
| [judo](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | JudoScript | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/judo?rev=236) / [LGPL](https://java.net/projects/scripting/sources/svn/content/trunk/engines/judo/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [juel](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  Java Unified Expression Language (JSP EL 2.1 JSR-245) | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/juel?rev=236) / [Apache License Version 2.0](https://java.net/projects/scripting/sources/svn/content/trunk/engines/juel/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [jython](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | Python の Java 実装 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jython?rev=236) / [Python Software Foundation License version 2](https://java.net/projects/scripting/sources/svn/content/trunk/engines/jython/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 192 | over 8 years ago | * Updated README.TXT files to re...
| [ognl](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | Object Graph Navigation Language | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/cgnl?rev=236) / [BSD](https://java.net/projects/scripting/sources/svn/content/trunk/engines/ognl/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [pnuts](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) | pnuts 言語 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/pnuts?rev=236) | tomatsu | 174 | almost 9 years ago | Issue number: Obtained from: Sub...
| [scheme](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  Scheme の Java 実装 | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/sceme?rev=236) / [Mozilla Public License Version 1.1](https://java.net/projects/scripting/sources/svn/content/trunk/engines/scheme/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 158 | about 9 years ago | Changed to use SISC version 1.1...
| [velocity](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  Java のテンプレートエンジン | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/velocity?rev=236) / [Apache License Version 2.0](https://java.net/projects/scripting/sources/svn/content/trunk/engines/velocity/THIRDPARTYLICENSES.TXT?rev=236) | sundararajana | 163 | about 9 years ago | Changed to use Velocity 1.5 ver...
| [xpath](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  XPath | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/xpath?rev=236) | sundararajana | 153 | over 9 years ago | Changed StringBuffer to StringB...
| [xslt](https://java.net/projects/scripting/sources/svn/show/trunk/engines/jaskell?rev=236) |  XSLT | [3-clause BSD](https://java.net/projects/scripting/sources/svn/show/trunk/engines/xslt?rev=236) | sundararajana | 136 | over 9 years ago | * Made makefile changes to buil... 

この表に名前はありませんが、　Clojure や Scala にも JSR-223 実装があります。

# 参考
1. [Java オブジェクトを操作するスクリプト言語比較](http://objectclub.jp/technicaldoc/java/scripting-java)