1

Want to execute a function in runtime, which function name is got it from runtime:

public static void callClassname() throws Exception
{
    String[] getExecute =getInputdata.getEligibleColoumn();
    for(int i=0 ; i < getExecute.length ; i++)
    {
        getExecute[i] = getExecute[i].concat("()");String test = "classname." .concat(getExecute[i]);
        System.out.println("Test Passed:" + test);
    }
}

in the above code test will give the result classname.functionname(). I want to execute it or call it. Could you please help me how to do it, or any other better way? plz suggest.

nalzok
  • 14,965
  • 21
  • 72
  • 139
Sathya
  • 11
  • 1

2 Answers2

0

The best way to solve this problem and to call instance methods is to use Java Reflection API

Cootri
  • 3,806
  • 20
  • 30
0

What you are looking for is convert String to code at runtime and you could use BeanShell to achieve this.

You could download it from here:

http://www.beanshell.org/download.html

You could also check out Java Compiler API:

https://www.javacodegeeks.com/2015/09/java-compiler-api.html

Mathews Mathai
  • 1,707
  • 13
  • 31