Monday, April 7, 2014

Hello from java-buddy, with nashorn JavaScript Engine.

JDK 8 introduces a new Nashorn JavaScript engine written in Java, which is an implementation of the ECMAScript Edition 5.1 Language Specification. Know more by reading Java Platform, Standard Edition Nashorn User's Guide.

it's a simple example to print hello message using "nashorn" ScriptEngine.



package javatestnashornjavascript;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestNashornJavascript {

    public static void main(String[] args) {
        
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
        
        try {
            nashorn.eval(
                "print('Hello from java-buddy, with nashorn JavaScript Engine.');");
        } catch (ScriptException ex) {
            Logger.getLogger(JavaTestNashornJavascript.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}


more nashorn JavaScript exercise:


No comments:

Post a Comment