Printing a Message on the Standard Output Using Nashorn - Java Scripting

Java examples for Scripting:Run Script

Description

Printing a Message on the Standard Output Using Nashorn

Demo Code

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

public class Main {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "print('Hello Scripting!')";
    try {//from w  w  w.j  ava  2  s  .co m
      engine.eval(script);
    }
    catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}

Result


Related Tutorials