Home » Core Java » script » Invoke specific script function

About Ilias Tsagklis

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

Invoke specific script function

This is an example of how to invoke a specific script function. We are using the ScriptEngine interface that provides methods for the basic scripting functionality. Invoking a script function with Java implies that you should:

  • Create a new ScriptEngineManager. 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 the getEngineByExtension(String extension) API method to look up and create a ScriptEngine for the js extension.
  • Create a String script that describes a function. In this example, we have created sayHi(name) function that prints a message and the name parameter ans another function, sayHi(), that invokes sayHi(name) function with a null parameter.
  • Use the eval(String script) method to execute the script.
  • Cast the engine to an Invocable engine, that is an interface implemented by ScriptEngines whose methods allow the invocation of procedures in scripts that have previously been executed
  • Use invokeFunction(String name, Object... args) method to call the functions defined in the 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;
import javax.script.Invocable;
 
public class InvokingFunctionExample {
    public static void main(String[] args) {

  String script =
"function sayHi() {" +
"   sayHi(null);" +
"}" +
" " +
"function sayHi(name) {" +
"   println('Hi' + " +
"
 ((name == null) ? ' JavaCodeGeeks Felow!' : ' ' + name + '!'));" +

"}";
 
  ScriptEngineManager manager = new ScriptEngineManager();

  ScriptEngine engine = manager.getEngineByExtension("js");
 
  try {
engine.eval(script);
 
//cast the engine to Invocable engine.

Invocable invocableEngine = (Invocable) engine;
 
// Invoke sayHi function without parameter.

invocableEngine.invokeFunction("sayHi");
 
// Invoke sayHi function with parameter.

invocableEngine.invokeFunction("sayHi", "username");

  } catch (ScriptException e) {

e.printStackTrace();

  } catch (NoSuchMethodException e) {

e.printStackTrace();

  }
    }
}

Output:

Hi JavaCodeGeeks Felow!
Hi username!

  
This was an example of how to invoke a specific script function in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

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 ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of