script

Get script engine by name

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 2nd, 2013
0 213 1 minute read

With this example we are going to demonstrate how to get theScriptEngine by name. The ScriptEngine interface is the fundamental interface whose methods must be fully functional in every implementation of this specification. These methods provide basic scripting functionality. Applications written to this simple interface are expected to work with minimal modifications in every implementation. It includes methods that execute scripts, and ones that set and get values. In short, to get the ScriptEngine by name you should:

  • Create a newScriptEngineManager. The ScriptEngineManager implements a discovery and instantiation mechanism for ScriptEngine classes and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager.
  • Use thegetEngineByName(String shortName) API method to look up and create aScriptEngine for a given name.
  • Then you can use theeval(String script) method to execute a script.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;import javax.script.ScriptEngineManager;import javax.script.ScriptEngine;import javax.script.ScriptException; public class GetScriptEngineByName {    public static void main(String[] args) {  ScriptEngineManager manager = new ScriptEngineManager();   // Create an instance of script engine using the engine name. In this example we use JavaScript  ScriptEngine engine = manager.getEngineByName("JavaScript");   try {engine.eval("print('Hello JavaCodeGeeks Fellows');");  } catch (ScriptException e) {e.printStackTrace();  }    }}

Output:

Hello JavaCodeGeeks Fellows

 
This was an example of how to get the ScriptEngine by name.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 2nd, 2013
0 213 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Import package in script

November 11th, 2012
Bipartite Graph

Modify Java object in script

November 11th, 2012
Bipartite Graph

Evaluate a simple script

November 11th, 2012
Bipartite Graph

Evaluate a script file

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.