What fancy ways to support scripts in java now?

I have a small program in which a user can enter a piece of "real" Java code in a text area, and then execute the code to perform some simple system tests. This program was created many years ago, and now I'm curious if there are any new bizarre ways to use the java platform to achieve the same (for example, if the user enters Groovy in the text area or maybe supports several script languages). I was curious when I read about java 7 that supports dynamically typed scripts in my virtual machine.

+5
source share
3 answers

You can use the built-in JavaScript support with Java 6: Creating a metalanguage using Java , see also ScriptEngineFactory.

In addition, Spring has Dynamic Language Support .

Please note that support for the dynamic JVM 7 language (via invokedynamic) does not matter here. It focuses primarily on dynamic languages ​​compiled into JVM bytecode (for example, JRuby or Groovy).

+3
source

ScriptEngineManagerwas introduced in java 1.6. This is the Sun version of the old old Jakarta BSF project that still exists. Both support a variety of scripting languages, including Groovy. The built- ScriptEngineManagerin only supports JavaScript, but I believe that you can add a Groovy interpreter.

+2
source

In addition to the scripting support added in newer versions of Java, you can use the Bean Shell , which can even be used with older versions of Java, such as 1.5. Using Bean Shell, you can simply:

// 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 - A fully compatible Java scripting engine for evaluating scripts and is used by Apache, Sun, Bea in their many products.

+1
source

All Articles