{"id":15916,"date":"2014-03-28T09:30:28","date_gmt":"2014-03-28T00:30:28","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=15916"},"modified":"2014-03-28T09:29:15","modified_gmt":"2014-03-28T00:29:15","slug":"java8%e3%81%ab%e5%90%8c%e6%a2%b1%e3%81%95%e3%82%8c%e3%81%a6%e3%81%84%e3%82%8bjavascript-engine-nashorn%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/15916.html","title":{"rendered":"Java8\u306b\u540c\u68b1\u3055\u308c\u3066\u3044\u308bJavaScript Engine Nashorn\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"<p>Java8\u304b\u3089\u65b0\u3057\u3044JavaScript Engine \u3067\u3042\u308bNashorn\u304c\u540c\u68b1\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u8a73\u7d30\u306f\u300c<a href=\"http:\/\/orablogs-jp.blogspot.jp\/2014\/03\/oracle-nashorn-next-generation.html\">Oracle Blogs \u65e5\u672c\u8a9e\u306e\u307e\u3068\u3081: [Java, JavaScript] Oracle Nashorn: A Next-Generation JavaScript Engine for the JVM<\/a>\u300d\u304c\u53c2\u8003\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n<!--more--><br \/>\n\u3055\u3063\u305d\u304f\u52d5\u304b\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u6b21\u306eJavaScript\u30d7\u30ed\u30b0\u30e9\u30e0\u3092hello.js\u3068\u3044\u3046\u540d\u524d\u3067\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: js; gutter: true\">\r\nvar hello = function() {\r\n  print(&quot;Hello Nashorn!&quot;);\r\n};\r\nhello();\r\n<\/pre>\n<p>Java8\u306b\u306fjjs\u30b3\u30de\u30f3\u30c9\u3068\u3044\u3046JavaScript\u5b9f\u884c\u7528\u30b3\u30de\u30f3\u30c9\u304c\u3042\u308b\u306e\u3067\u3001\u3053\u308c\u3067\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">\r\n$ jjs hello.js \r\nHello Nashorn!\r\n<\/pre>\n<p>JavaScript\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092Java\u30d7\u30ed\u30b0\u30e9\u30e0\u5185\u3067\u5229\u7528\u3059\u308b\u306b\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u7c21\u5358\u3067\u3059\u306d\u3002\u3053\u3053\u3067\u306f\u3001js_src\u306bJavaScript\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8a2d\u5b9a\u3057\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">\r\nimport javax.script.ScriptEngine;\r\nimport javax.script.ScriptEngineManager;\r\n \r\npublic class Sample {\r\n  public static String js_src = &quot;function sum(a, b) { return a+b; }&quot;;\r\n  public static void main(String... args) throws Throwable {\r\n    ScriptEngineManager engineManager = new ScriptEngineManager();\r\n    ScriptEngine engine = engineManager.getEngineByName(&quot;nashorn&quot;);\r\n    engine.eval(js_src);\r\n    System.out.println(engine.eval(&quot;sum(2, 5);&quot;));\r\n  }\r\n}\r\n<\/pre>\n<p>\u6b21\u306e\u3088\u3046\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">\r\n$ javac Sample.java\r\n$ java Sample\r\n7\r\n<\/pre>\n<p>Nashorn\u306e\u7528\u9014\u3068\u3057\u3066\u306f\u3001Java\u30a2\u30d7\u30ea\u3067\u62e1\u5f35\u6a5f\u80fd\u3092JavaScript\u3067\u8a18\u8ff0\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u305f\u3044\u3001\u3068\u3044\u3046\u611f\u3058\u3060\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001node.js\u307f\u305f\u3044\u306b\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3068\u51c4\u3044\u306e\u3067\u3059\u304c\u3001\u3069\u3046\u306a\u306e\u3067\u3057\u3087\u3046\u3002\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30a4\u30c9\u3067\u4f7f\u3048\u308b\u305f\u3081\u306b\u306fWeb\u30d6\u30e9\u30a6\u30b6\u306e\u30a8\u30f3\u30b8\u30f3\u3068\u3057\u3066\u642d\u8f09\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u305d\u3046\u3067\u3059\u304c\u3001\u305d\u3061\u3089\u306e\u8a71\u984c\u306f\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3001Java\u5c02\u7528\u306e\u7279\u5225\u306a\u30a8\u30f3\u30b8\u30f3\u3068\u3044\u3046\u4f4d\u7f6e\u3065\u3051\u3067\u5229\u7528\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java8\u304b\u3089\u65b0\u3057\u3044JavaScript Engine \u3067\u3042\u308bNashorn\u304c\u540c\u68b1\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u8a73\u7d30\u306f\u300cOracle Blogs \u65e5\u672c\u8a9e\u306e\u307e\u3068\u3081: [Java, JavaScript] Oracle Nashorn: A &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/15916.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[1207,31,700],"class_list":["post-15916","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-javascript","tag-program-2"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/15916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=15916"}],"version-history":[{"count":3,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/15916\/revisions"}],"predecessor-version":[{"id":15919,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/15916\/revisions\/15919"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=15916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=15916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=15916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}