0

I've created a Talend job that should be loaded in runtime. I'm loading job jar dynamically in code. After loading it, I need to invoke a function that will execute the job.

To performed it, I've followed the answers of this question. But when the function is invoked I'm getting java.lang.NoSuchMethodException. I think the problem is in the function's parameters type definition, but I have troubles to define it correctly.

Here is my code:

String args[] = new String[7];
args[0] = "myParams";

File jobJar = new File("myjar.jar");
URL [] urls = new URL[1];
urls[0] = jobJar.toURI().toURL();

Class<?>[] params_type = new Class[]{args.getClass()}; //is it correct?

URLClassLoader child = new URLClassLoader(urls , this.getClass().getClassLoader());
Class classToLoad = Class.forName ("com.my.myTalendClass", true, child);
Method method = classToLoad.getDeclaredMethod ("runJobInTOS", params_type);
Object instance = classToLoad.newInstance();
Object result = method.invoke(instance,new Object[]{ args });

and the function runJobInTOS receives as parameters an array of Strings

Community
  • 1
  • 1
andriy
  • 4,074
  • 9
  • 44
  • 71

1 Answers1

1

Why do you use

Object result = method.invoke(instance,new Object[]{ args });

But not

Object result = method.invoke(instance,args);

In your code you pass a two-dimensional array to the method, not an ordinary array of Strings

  • passing parameters as you have proposed gives error of `wrong number of arguments` – andriy Sep 11 '13 at 15:36
  • have you posted any link to article? – andriy Sep 12 '13 at 08:08
  • http://yourmitra.wordpress.com/2008/09/26/using-java-reflection-to-invoke-a-method-with-array-parameters/ sorry –  Sep 12 '13 at 12:12
  • Thank you. This link was helpful! But I've also changed `args.getClass()` to `String[].getClass()`.. do not know what the difference. – andriy Sep 12 '13 at 16:39