我创建了一个应该在运行时加载的 Talend 作业。我正在代码中动态加载作业 jar。加载后,我需要调用一个将执行作业的函数。
为了执行它,我遵循了这个问题的答案。但是当调用该函数时,我得到java.lang.NoSuchMethodException
. 我认为问题出在函数的参数类型定义中,但我无法正确定义它。
这是我的代码:
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 });
该函数runJobInTOS
接收一个字符串数组作为参数