Java 使用反射从jar调用类的方法
我想使用反射从外部jar中的类调用一个方法。但我还是做不到。我正在使用以下代码Java 使用反射从jar调用类的方法,java,class,reflection,jar,invoke,Java,Class,Reflection,Jar,Invoke,我想使用反射从外部jar中的类调用一个方法。但我还是做不到。我正在使用以下代码 public class Test { public static void main(String[] args) throws Exception { try { // File JarFile = new File("C:\\dataRetrieval.jar"); // // URLClassLoader cl = new URLClass
public class Test {
public static void main(String[] args) throws Exception {
try {
// File JarFile = new File("C:\\dataRetrieval.jar");
//
// URLClassLoader cl = new URLClassLoader(new
// URL[]{JarFile.toURI().toURL()});
// JarFile.getClass();
DataRetrievalManagerServiceStub stub = new DataRetrievalManagerServiceStub(
"html//...............???wsdl");
Map<String, String> requestMap = new HashMap<String, String>();
requestMap.put("TelephoneNumber", "01028824332");
Object stubInstance = Class.forName("com.test.DataRetrievalManagerServiceStub").newInstance();
Object reqInstance = Class.forName("com.test.impl.GetOllehIdsForUserDocumentImpl").newInstance();
Method[] stubMethods = stubInstance.getClass().getMethods();
Method[] reqMethods = reqInstance.getClass().getMethods();
//Method[] resMethods = resInterface.getClass().getMethods();
for (Method method : reqMethods) {
String methodName = method.getName();
System.out.println(methodName);
}
for (Method method : reqMethods) {
String methodName = method.getName();
if (methodName.startsWith("set")) {
methodName = methodName.replace("set", "");
String value = null;
if (requestMap.containsKey(methodName)) {
value = requestMap.get(methodName);
}
method.invoke(reqInstance, value);
}
}
Object resInterface = Class.forName(
"com.test.GetOllehIdsForUserResponseDocument").getInterfaces();
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
有人知道吗?你发的太多太少了。发送完整堆栈跟踪。jar结构并不有趣。并发送生成异常的行代码。之前可能有5行。如果您删除了带有错误的行,请给我们完整的跟踪。InstanceionException意味着您在没有公共构造函数也没有参数的类上调用newInstance。有关更多信息,请阅读newInstance的javaDoc。或者在instantiation.OP已经有问题时引发异常:@kkoyu请删除这些评论并将格式化的跟踪添加到您的帖子中,此处无法阅读。
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisOperation). log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.InstantiationException: com.kt.sdp.impl.GetOllehIdsForUserDocumentImpl
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source) at sdk.kt.com.Test.main(Test.java:78)