Reputation: 981
I have a java program that processes information, but I want to make it so the end user can write javascripts that dictate what to do with this info. Like this
//Java
private void newData(int var1) {
script.newData(var1);
}
and then
//Javascript
function newData(var var1) {
someVar = var1;
processVar();
}
I have looked into something called rhino, but I really am having trouble understanding the concept of rhino. Anyone know what to do?
Upvotes: 2
Views: 95
Reputation: 31631
You want Rhino. Rhino is a javascript runtime implemented in Java. It is suitable for embedding in Java applications.
What you want to do is create your Java classes and objects and then make them accessible to a Javascript environment. Fortunately this is very easy with Rhino. Read this tutorial and pay close attention to the first and second sections (RunScript: A simple embedding and Expose Java APIs).
The first section is about executing Javascript within a Java application. You will need to adapt their sample code a little to provide some way for the end user to hand javascript code to you (in a file or stream) for you to execute. It won't be difficult.
The second section is about making your Java stuff available to the Javascript stuff. In the simplest case you don't need to do anything--all of Java is available to Rhino javascript automatically. But you can very easily pretty up the interface and provide something easier for the end user to use if you want.
With Rhino you can also go in the other direction--you can make Javascript objects available to the Java environment. This is a little more complicated, but is covered in the rest of the tutorial. You may not need to do this either.
Upvotes: 5