Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top