5

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.

asked Jan 9, 2012 at 7:19

3 Answers 3

3

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).

answered Jan 9, 2012 at 7:25
Sign up to request clarification or add additional context in comments.

1 Comment

Note that not all Java 6 JVM's ship with JavaScript - just ran into an IBM-one that didn't.
2

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.

answered Jan 9, 2012 at 7:24

Comments

1

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.

answered Jan 9, 2012 at 7:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.