安装

  • 地址
http://www.graalvm.org/downloads/
  • 配置环境变量
linux:
export PATH=/path/to/graalvm/bin:$PATH
mac:
export PATH=/path/to/graalvm/Contents/Home/bin:$PATH

基本代码编写

import java.io.*;
import java.util.stream.*;
import org.graalvm.polyglot.*;

public class PrettyPrintJSON {
  public static void main(String[] args) throws java.io.IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String input = reader.lines().collect(Collectors.joining(System.lineSeparator()));
    try (Context context = Context.create("js")) {
      Value parse = context.eval("js", "JSON.parse");
      Value stringify = context.eval("js", "JSON.stringify");
      Value result = stringify.execute(parse.execute(input));
      System.out.println(result.asString());
    }
  }
}

参考架构图

参考资料

http://www.graalvm.org/docs/getting-started/#combine-languages