0

Is it possible to execute an array of code in the form of stings? eval() does not work for I am executing this in RFT and eval is for equations not code itself.

Here is an example to show what I am getting at:

private String[] code = new String[20];
int x = 0;

code[0] = "System.out.print("Hello World\n");"; 
code[1] = "x++;";
code[2] = "System.out.print(x);";

for(String s : code){
  //execute the string as if it were code
}

So output would be

Hello World
1
TJF
  • 45
  • 6
  • possible duplicate of [Is there an eval() function in Java?](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Madhawa Priyashantha Jul 29 '15 at 14:37
  • Java? Nope. You can execute JavaScript, though. – Florian Schaetz Jul 29 '15 at 14:37
  • That would be for evaluation. Hard to believe there's no way of doing this. – TJF Jul 29 '15 at 14:41
  • @TJF what is RFT ?? and what do you trying to do ?may be there is a alternative way – Madhawa Priyashantha Jul 29 '15 at 14:46
  • Rational Functional Tester by IBM. I am doing regression testing and would like to be able to add code to say a text file and have the program run though it without having to go into the Java and add the code. So the text file would have code to test a link and if I wanted to test another link I could just append it onto the file and not have to go into the java code. – TJF Jul 29 '15 at 14:49
  • @TJF java has memory compiler .using memory compiler you can run a java class – Madhawa Priyashantha Jul 29 '15 at 14:52
  • So the file would be like this, link1().click(); and the program would test to see if the link is clickable. Then I could add to the file link2().click(); and it would then also test that link. As of now if I wanted to test link1 and not link2, I would need to go into the code and edit it. – TJF Jul 29 '15 at 14:52
  • Not to my knowledge, I do not think there is any way in which you somehow parse x++ on its own and pass it into a value as each element must be of a certain type. Therefore, for those reasons Java couldn't do it. The man reason I see for there not being a way to do this as such, is that there is no point in doing so Java-wise. – SomeStudent Jul 29 '15 at 14:53
  • Java isn't a scripting language so it can't just execute snippits of code. If you properly format it into a class, you can compile that class and then load it into the classloader. This is probably more effort than it's worth though. (http://www.javabeat.net/the-java-6-0-compiler-api/). Your best bet is to look into using javascript or groovy with the script engine. You will still have access to all of your java classes but be able to script them easily. – Tea Curran Jul 29 '15 at 14:59
  • Thanks for all the replies! I'll look into groovy. Hope I can get this working somehow. – TJF Jul 29 '15 at 15:05

1 Answers1

2

If you are using Java 6 or higher you might want to consider the Java Compiler API found here: http://www.javabeat.net/the-java-6-0-compiler-api/. With the API you should be able to compile and run code that is represented as a String.

Here is a similar question someone asked on the subject that includes code in the answer:

Convert String to Code

Hope this helps!

Community
  • 1
  • 1
jhhurwitz
  • 89
  • 5