Reputation: 199
I am developing a java app where the user inputs certain functions through a property files. The functions are then evaluated at runtime with variable values provided by the app.
Here a simple example. The user wants to calculate the return on investment for a certain capital allocation over the next n years. He should be able to input not just relevant parameters, but a function that calculates the return year by year.
Can anyone suggest or recommend a scripting language which is simple , so that the user learning curve is not too steep, and for which a standard library providing integration with java code is available?
A related example would be welcome too.
Upvotes: 0
Views: 995
Reputation: 7998
My suggestion is to use ScriptEngine
and evaluate Javascript
functions. As simple as:
public class TestScriptEngine {
private static final String script = "var customFunction = function(parameter) {\n" +
"\treturn parameter*parameter;\n" +
"};\n" +
"\n" +
"println(customFunction(10));\n";
public static void main(String[] args) {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code User's input
try {
engine.eval(script);
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 4266
You could take a look at Groovy. It can be embedded in Java applications and be a useful tool for scripting in applications.
http://groovy.codehaus.org/Embedding+Groovy
Upvotes: 1
Reputation: 1037
You could use JavaScript or Ruby, they should be convenient enough.
Upvotes: 0
Reputation: 5067
I would suggest using javascript. It is simple enough and the Rhino library is integrated with java. Complete information can be found here: Rhino
Upvotes: 1