Reputation: 557
I have a small program where the user can enter a snippet of "real" java code in a text area, and then execute the code in order to perform some simple system tests. This program was made ages ago, and now I am curious if there are some new fancy ways to utilize the java platform in order to achieve the same thing (by for instance having the user entering Groovy in the text area, or perhaps supporting several script languages). I got curious when I read about java 7 having support for dynamically typed scripts in its virtual machine.
Upvotes: 5
Views: 455
Reputation: 786359
Apart from the scripting support added in newer versions of Java you have an option to use Bean Shell, something which can be even used with older Java versions such as 1.5. Using Bean Shell
you can simply do:
// assuming you have Java code in a string called script, you can do
Object result = new bsh.Interpreter().eval(script);
// now result object will have the result of your Java code contained in string script
Bean Shell
is is fully Java compatible scripting engine for evaluating scripts and is used by Apache, Sun, Bea in their many products.
Upvotes: 1
Reputation: 341003
You can use JavaScript support built-in from Java 6: Creating meta language with Java, see also ScriptEngineFactory
.
Also Spring framework has a Dynamic language support.
Note that the JVM 7 dynamic language support (via invokedynamic
) is irrelevant here. It is primarily targeted to dynamic languages compiled to JVM bytecode (like JRuby or Groovy).
Upvotes: 3
Reputation: 115418
ScriptEngineManager
was introduced in java 1.6. It is the Sun's version of good old Jakarta BSF project that still exists. Both support variuous scripting languages including Groovy. The built-in ScriptEngineManager
supports JavaScript only but I believe you can add Groovy interpreter too.
Upvotes: 2