11

Similar to dynamic SQL, wherein a String is executed as an SQL at runtime, can we have Java code run dynamically? Like I return a String which is a Java code and then I execute at runtime. Is this possible?

6 Answers 6

16

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

The easiest way to execute code supplied at runtime would be to use the Rhino JavaScript engine.

Both of these options have been only in Java 6, though I believe the scripting interface existed before, so you could use Rhino in an earlier JRE if you download and add it to the classpath.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 on this, I've added Javascript interpreting to a few apps, it's fairly painless and very powerful.
The Rhino JavaScript engine can't execute Java code at runtime.
@AndersonGreen: you actually can import Java classes and use them as JavaScript objects, but I wrote that mainly for cases where the focus is on being able to easily run code dynamically, not that it must be Java code.
6

Javassist

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

So it is possible, but it's very heavyweight and is likely to make your application very hard to reason about. If at all possible, consider designing a very flexible class statically, and having it accept parameters that control its behaviour.

1 Comment

+1 -- for a comprehensive list of possible libraries: java-source.net/open-source/bytecode-libraries
4

If you want to do more than invoke an existing method dynamically, you may need to compile your String into bytecode. An easy way to do this is to include the Eclipse/JDT compiler jar in your classpath, and then you can use that to compile your String into a Class, which can then be loaded.

This type of dynamic code generation and execution is used to convert JSP files into Servlets and is used in other packages such as JasperReports to turn a report specification into a Class that is then invoked.

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

2 Comments

Good point to mention the "injection" problems, though I dare say this entire architecture is injection by definition - you're trying to execute this arbitrary code, so I would hope it goes without saying that the source should be trusted implicitly.
A nice thing with Java is that you can run the injected code with limited privileges, too.
4

You also may want to look at Java 6 scripting support: http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.htm

Here is a version of hello world that creates array of strings and prints a first one:


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])");
    }
}

Comments

3

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

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

Comments

3

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

Comments

Your Answer

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.