How to Call External JavaScript Functions From Java Code

How to Call External JavaScript Functions From Java Code

This article will discuss including a JavaScript source in a Java program and calling a JavaScript function from a Java code.

Call External JavaScript Functions From Java Code

Calling a JavaScript source in Java is pretty simple. If we develop a Java application in which we must use JavaScript, we will create the script file separately, then include and call it in the Java source to run the desired function.

Let us have a look at the following scenario as an example.

First, we need to define functions in a separate JavaScript file which will be called in our Java code. We created a JavaScript source to perform arithmetic tasks like adding two passed values.

Example:

var myCalculations = new Object();

myCalculations.addition = function(v1, v2) v1 + v2;

In this JavaScript source, we created a new object, myCalculations. In that object, we have a defined function of addition that will take 2 values as an argument and add them.

We need to save this source with the extension .js as this will be our JavaScript document. To load that script and get the object, we will require to use the eval() function of ScriptEngineManager.

ScriptEngineManager is a default Java library that can be imported into a Java code. We can use an instance of that library to load the scripts.

We can load the JavaScript file, as shown below:

String javascriptPath = "drive:/folder/fileName.js";
engine.eval("load('" + javascriptPath + "')");
Object myCalculations = engine.get("myCalculations");

Now we can get the desired function by name and pass in the parameters, as shown below:

Object addingResult = inv.invokeMethod(myCalculations, "addition", x, y);

Here is an example of a complete Java program that will use a JavaScript file using ScriptEngineManager.

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Main {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    Invocable inv = (Invocable) engine;
    String javascriptPath = "drive:/folder/fileName.js";
    engine.eval("load('" + javascriptPath + "')");
    Object myCalculations = engine.get("myCalculations");

    int x = 10;
    int y = 5;
    Object addingResult = inv.invokeMethod(myCalculations, "addition", x, y);

    System.out.println("Your addition result will be: " + addingResult);
  }
}

Output:

Your addition result will be: 15

Code explanation:

  1. In the above Java code, we first imported a useful library, ScriptEngineManager, to load the script.
  2. In the main function, we have created an instance of ScriptEngineManager and defined the Script engine with the name of JavaScript.
  3. Now, we have declared a string variable that will be assigned with the path of the JavaScript file.
  4. Then, we load that path with the engine.eval() method.
  5. Now, we have called the object myCalculations of the JavaScript file and used it to invoke the function of that object.
  6. Finally, we passed 2 integer type values in the function and displayed the result in the terminal using the Java default function, System.out.println("anything you want to print").