Nashorn is a JavaScript engine included in Java that allows you to execute JavaScript code within a Java application. One of the powerful features of Nashorn is the ability to access Java classes and methods directly from JavaScript code. This allows you to leverage existing Java libraries and use them seamlessly in your JavaScript code. In this blog post, we will explore how to access Java classes and methods from Nashorn.
Table of Contents
- Getting Started with Nashorn
- Accessing Java Classes from Nashorn
- Calling Java Methods from Nashorn
- Passing and Returning Java Objects
- Conclusion
Getting Started with Nashorn
Nashorn is included in Java 8 and later versions, so you don’t need to add any additional dependencies to your project. To start using Nashorn, you simply need to import the javax.script
package and create a ScriptEngine
instance.
import javax.script.*;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Now you have a Nashorn engine instance ready to execute JavaScript code.
Accessing Java Classes from Nashorn
To access Java classes from Nashorn, you can use the Java.type()
function. The Java.type()
function takes the fully qualified name of the Java class as a string parameter and returns a JavaScript object representing that class.
var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList();
list.add('Hello');
list.add('World');
print(list.size()); // Output: 2
In the above example, we access the java.util.ArrayList
class using Java.type()
and create an instance of it. We then add some elements to the list and print its size.
Calling Java Methods from Nashorn
Once you have a Java object in Nashorn, you can call its methods directly from JavaScript code using the dot notation.
var LocalDateTime = Java.type('java.time.LocalDateTime');
var now = LocalDateTime.now();
print(now.toString()); // Output: 2022-01-01T12:30:15.123
In the above example, we access the java.time.LocalDateTime
class and call its now()
method to get the current date and time. We then print the result using the toString()
method.
Passing and Returning Java Objects
Nashorn automatically converts JavaScript objects to Java objects and vice versa when passing them between the two environments. This means you can pass Java objects as arguments to JavaScript functions and return Java objects from JavaScript functions.
var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList();
list.add('Hello');
list.add('World');
var size = function(list) {
return list.size();
};
print(size(list)); // Output: 2
In the above example, we define a JavaScript function size()
that takes a Java ArrayList
as an argument and returns its size. We pass the list
object created earlier to the size()
function and print the result.
Conclusion
Accessing Java classes and methods from Nashorn allows you to leverage the power of existing Java libraries in your JavaScript code. You can easily access Java classes, call methods, and pass/receive Java objects seamlessly. This makes Nashorn a powerful tool for integrating Java and JavaScript in your applications.
With the ability to access Java classes and methods from Nashorn, you can take advantage of the vast Java ecosystem and combine it with the flexibility of JavaScript. This opens up new possibilities for developing powerful and scalable applications.
#java #javascript