Dynamic code execution

Like dynamic SQL, where String is executed as SQL at runtime, can we run Java code dynamically? How do I return String, which is Java code, and then I execute at runtime. Is it possible?

+7
java
source share
7 answers

For real Java code, this is possible using the JavaCompiler . However, this is very inconvenient to use, as it is just an interface to a real Java compiler that expects to compile the whole class definitions found in files.

The easiest way to execute the code provided at runtime is to use the Rhino JavaScript engine .

Both of these parameters were only in Java 6, although I believe that the scripting interface existed before, so you can use Rhino in the early JRE if you load and add it to the classpath.

+14
source share

Javassist

You will need to use a bytecode manipulation library such as Javassist ( Wikipedia ) to run an arbitrary string that is provided at runtime. Javassist allows you to create a CtClass based on a string representing the source code; and then can turn this into a compiled Class object through a specific class loader so that the class is accessible to your application. Other libraries would have to do something similar to these two steps in order to achieve the same.

So it’s possible, but it’s very heavyweight and probably very difficult for your application. If at all possible, consider creating a very flexible class statically and accepting its parameters that control its behavior.

+5
source share

If you want to do more than dynamically use an existing method, you may need to compile your String into bytecode. An easy way to do this is to include the Eclipse / JDT compiler cube in your class path, and then you can use it to compile your String into a class, which you can then load.

This type of dynamic code generation and execution is used to convert JSP files to servlets and is used in other packages, such as JasperReports, to convert the report specification to a class, which is then called.

Remember that as with SQL, you must be careful to prevent code entry security issues if any of the String contains user-specified data.

+4
source share

Take a look at Beanshell . It provides an interpreter with java syntax.

+3
source share

You can also see Java 6 script support: http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.htm

Here is the version of hello world that creates an array of strings and prints the first:

 import javax.script.*; public class EvalScript { public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.eval("var a=java.lang.reflect.Array.newInstance(java.lang.String, 1);a[0]='Hello World';print(a[0])"); } } 
+3
source share

Yes it is possible. Take a look at the Java Compiler API. Look at here:

http://download.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

+2
source share

Yes, using reflection: tutorial

And another tutorial from IBM

-one
source share

All Articles