8

Possible Duplicate:
Convert String to code in Java
Dynamic code execution on Java

I have a String containing :
"for(int i=0 ; i<5 ; i++){System.out.println(\"*\");}"
Can I execute the code in this String in Java?

4
  • You can use the JavaCompiler interface, but I don't know the details. Commented Dec 28, 2011 at 21:46
  • If you have the choice don't use java as input use JavaScript and run your code through the Java Scripting API Commented Dec 28, 2011 at 21:50
  • What is the importance of this code? Do you use this code in some real applications? Commented Dec 28, 2011 at 21:59
  • @Lion - Yes I am using this in a real application. But now I am using a String of JavaScript code instead of Java code. Commented Dec 30, 2011 at 5:24

6 Answers 6

5

Since Java 6, you can compile and run a Java compilation unit defined as a String or a File using standard APIs in the SDK (a compilation unit is basically everything that goes inside a .java file - package, imports, classes/interfaces/enumerations), take a look at this example. You can't run an arbitrary Java snippet like the one in your question, though.

If at all possible, it'd be a better idea to embed a different scripting language that allows you to run snippets of code from a Java program - for example, JavaScript, Groovy, MVEL, BeanShell, etc.

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

Comments

2

If you turn it into a full-blown source file, you can feed it to the java compiler programmatically, but last time I checked that was only available if you had the java SDK installed on your machine; it was not available on machines with the client distribution of Java. Of course, this may have changed since then. Look at package com.sun.tools.javac and you will find the java compiler API there.

Comments

2

Maybe you can run this as Groovy:

http://groovy.codehaus.org/Embedding+Groovy

Comments

1

There isn't a Java Core API function for doing this, but you can call javac either by using Runtime.exec or using some "unsafe" classes from com.sun.tools.javac Here's an example:

http://juixe.com/techknow/index.php/2006/12/12/invoke-javac-at-runtime/

Comments

1

I don't think you can execute a String containing a java code.

But it is worth a try if you can save that as a java source file and try to use ProcessBuilder class to execute.

Never tried it and not sure if it is best way to do it. So use it with caution :)

Good Luck!

Also found a similar post: Runtime class in java

Comments

0

No, you can not execute this code in your program.

1 Comment

Yeah, there are exceptions, but it's an "If you have to ask" sort of thing -- if you don't already understand that it's impossible then it's impossible. If you understand that it's impossible (and why) then it becomes possible (at least in some circumstances), using some combination of several dirty tricks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.