var pr = new ConsoleRunner(); pr.exec("python --version");
First of all, we need to start with a fresh Java project. I use a simple maven project for this demo.
Add a dependency for Rhino into pom.xml
<dependencies> <dependency> <groupId>cat.inspiracio</groupId> <artifactId>rhino-js-engine</artifactId> <version>1.7.10</version> </dependency> </dependencies>
Let’s try to evaluate this simple snippet
var a = [2,8,5]; a + a;
First line simply assigns[2, 8, 5] array to the identifier “a”. Eventually we do a simple expression a + a. So the evaluated end-result should be 10
Create RhinoExample.java as per below.
When you execute this it will give10as the output as we expected before.
Creating a host object
Create ConsoleRunner.java by extending with the ScriptableObject class
Here are some important facts about the above source,
– Overridden getClassName method should always return the current class name.
Let put all the stuff together
We need to modify RhinoExample.java by adding our sample code snippet and also by adding a reference to our host object class.
Find Shalitha Suranga on Medium for more tutorials and articles
Happy Coding 👨💻