0

Possible Duplicate:
Convert String to code

I have a program that needs to be able to execute a string as code. For example, I would have a string that reads:

public void do(int a, int b){
a++;
b--;
System.out.println(a);
System.out.println(b);
}

Now, how do I get the program to execute that string?

Community
  • 1
  • 1
Steven
  • 1,709
  • 3
  • 17
  • 27
  • 1
    what have you tried? and why do you want to do something like that if you dont mind me asking? – FSP Oct 04 '12 at 03:48
  • related: http://stackoverflow.com/questions/2605032/using-eval-in-java – lc. Oct 04 '12 at 03:48
  • I believe you are looking for something similar like [this](http://stackoverflow.com/questions/935175/convert-string-to-code). – Yogendra Singh Oct 04 '12 at 03:49
  • [ideone](http://ideone.com) does this all the time. – aroth Oct 04 '12 at 04:02
  • @aroth that's exactly what I'm talking about. I'm going to be doing a very similar thing with this. – Steven Oct 04 '12 at 04:10
  • @StevenFontaine ideone runs javac to compile the code submission and *then* runs it. – FThompson Oct 04 '12 at 04:11
  • 1
    @Vulcan - It's probably a bit more complicated than that. I assume they must have some sort of sandbox or custom runtime to prevent malicious code from running amok. – aroth Oct 04 '12 at 04:13
  • @aroth They simply run the JVM with the `-Djava.security.manager="com.example.CustomSecurityManager"`, and that custom security manager handles security. – FThompson Oct 04 '12 at 04:33
  • @Vulcan - Yep, that works well enough for Java/JVM-based languages, but ideone also supports compiled languages like C/C++. – aroth Oct 04 '12 at 04:46
  • @aroth OP is only asking about Java, so setting the security manager property via the command line is a good solution. – FThompson Oct 04 '12 at 04:49

5 Answers5

1

You can wrap this string in a class and compile it with Java Compiler API. You would have to be running JDK instead of JRE.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
0

See this similar question

This simple answer is, this can't really be done in Java. You could come up with some hacky ways of doing it by attempting to invoke the javac compiler, but I'm not sure how you would really load it in or make use of it at that point.

In some scripting languages, like Javascript and Lua, you can use 'eval' type functions that attempt to load your string and execute it.

Community
  • 1
  • 1
shortstuffsushi
  • 2,271
  • 19
  • 33
  • Alright, what if I put the string into a file that the code would then read and execute? Could that be done? And if so, how would I go about doing it? (I hate it when I get stuck on something like this!) – Steven Oct 04 '12 at 03:52
  • Just to clarify, when I said, "how would I go about doing it," I meant running the created file. Not how to create a file with java.. I know how to do that. :P – Steven Oct 04 '12 at 03:54
  • At that point, you'd want to look at the Compiler API, as Denis referred to below. You can see another similar question here. http://stackoverflow.com/questions/1064259/how-can-i-compile-and-deploy-a-java-class-at-runtime – shortstuffsushi Oct 04 '12 at 03:58
  • I can't use Java Compiler API for a few reasons... Maybe if I explain why I need this you can help me think of a solution.. Basically, I am writing a program where other people will be writing code in it that needs to be able to be executed. Of course, if you use a text field or whatever, the text is a string and I need that string to be executed.. Which presents a dilemma.. – Steven Oct 04 '12 at 04:05
  • Does it have to be Java code they're writing? If it's just example code, make them write it in Javascript and just run an eval on it. If it's got to be in Java, you're going to have to use a tool to compile it -- you can't just evaluate it (unless you want to write your own parser) – shortstuffsushi Oct 04 '12 at 05:25
0

I'm not entirely sure (it has been a long time since I've used it), but you might be able to use BeanShell to execute the Java code that is a String. Take a look at the docs for some examples.

Eric B.
  • 23,425
  • 50
  • 169
  • 316
0

I think the question is how do I run this program? The answer is it needs to be wrapped in a class definition, along with a "static main" function so the java command line tool can run it after it is compiled.

ddyer
  • 1,792
  • 19
  • 26
0

Take a look at Koala Dynamic Java which takes text and dynamically compiles and runs it.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
  • this seems to be dead since 2002, but I followed a trail of breadcrumbs to DrJava which incorporates and extends Dynamic Java – ddyer Oct 05 '12 at 03:34