我编写了一个应用程序,它调用一个放在我的类路径之外的jar文件,然后调用java提供的URLCLassLoader api中的方法。 我可以从没有参数的jar调用方法,但是当我尝试顶部调用一个接受参数的方法时,我得到了#34;没有这样的方法"例外
java.lang.NoSuchMethodException: plugins.PluginTest.sendOTP(com.ca.pas.plugin.bean.RequestBean)
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at com.ca.pas.plugin.controller.PluginController.invokePlugin(PluginController.java:45)
at com.ca.pas.plugin.service.PluginServiceImpl.sendOTP(PluginServiceImpl.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
package plugins;
public class PluginTest {
public ResponseBean sendOTP(RequestBean rbs){
System.out.println(rbs.getOrb().getMobileNumber());
ResponseBean rb = new ResponseBean();
return rb;
}
}
上面的jar文件定义了自己的requestBean类。
package com.ca.pas.plugin.service;
import org.springframework.stereotype.Service;
import com.ca.pas.plugin.bean.RequestBean;
import com.ca.pas.plugin.bean.ResponseBean;
import com.ca.pas.plugin.controller.PluginController;
/**
* @author jamju02
*
*/
@Service("pluginService")
public class PluginServiceImpl implements PluginService{
@Override
public ResponseBean sendOTP(RequestBean requestBean) {
PluginController pc = new PluginController();
return pc.invokePlugin(requestBean);
}
}
package com.ca.pas.plugin.controller;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import com.ca.pas.plugin.bean.RequestBean;
import com.ca.pas.plugin.bean.ResponseBean;
/**
* @author jamju02
*
*/
public class PluginController {
/**
* This method is used to invoke plugin class dynamically
* @param requestBean : This object contains all parameter to
* complete the request
* @return responseBean : This object contains the response from vendor
*/
public ResponseBean invokePlugin(RequestBean requestBean){
URLClassLoader urlClassLoader = null;
ResponseBean responseBean = null;
Object[] obj = {new RequestBean() };
Class params[] = new Class[obj.length];
try {
urlClassLoader = new URLClassLoader(new URL[]{new URL("file:///C:/Users/jamju02/Desktop/today/otp.jar")});
if(urlClassLoader != null){
Class pluiginClass = urlClassLoader.loadClass("plugins.PluginTest");
if(pluiginClass != null){
// Create a new instance from the loaded class
Constructor<?> constructor = pluiginClass.getConstructor();
Object classobject = constructor.newInstance();
params[0] = RequestBean.class;
Method method = pluiginClass.getDeclaredMethod("sendOTP", params);
method.setAccessible(true);
responseBean = (ResponseBean) method.invoke(classobject);
//System.out.println((String) method.invoke(classobject));
}else{
System.out.println("class file name not found in jar file");
}
}else{
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}finally{
try {
if(urlClassLoader != null){
urlClassLoader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseBean;
}