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
10
  • possible duplicate of Is there an eval() function in Java? Commented Jul 29, 2015 at 14:37
  • Java? Nope. You can execute JavaScript, though. Commented Jul 29, 2015 at 14:37
  • That would be for evaluation. Hard to believe there's no way of doing this. Commented Jul 29, 2015 at 14:41
  • @TJF what is RFT ?? and what do you trying to do ?may be there is a alternative way Commented Jul 29, 2015 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. Commented Jul 29, 2015 at 14:49

1 Answer 1

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!

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

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.