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?

Community
  • 1
  • 1
Devashish Dixit
  • 1,153
  • 6
  • 16
  • 25
  • You can use the [`JavaCompiler`](http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html) interface, but I don't know the details. – Oliver Charlesworth Dec 28 '11 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](http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/) – Sebastian Annies Dec 28 '11 at 21:50
  • What is the importance of this code? Do you use this code in some real applications? – Lion Dec 28 '11 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. – Devashish Dixit Dec 30 '11 at 05:24

6 Answers6

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
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.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
2

Maybe you can run this as Groovy:

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
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/

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
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

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

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

Balconsky
  • 2,234
  • 3
  • 26
  • 39
  • 2
    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. – Hot Licks Dec 28 '11 at 21:53