我正在尝试从.jar
调用方法但是当我想为getDeclaredMethod声明params数组时,我发现错误“无法找到符号”
Class[] params = new Class[]{ String };
^
整个方法
public void CallCloseWindow(String title){
//export dll
InputStream in = staticapi.class.getResourceAsStream("CloseWindow.dll");
File fileOut = new File(System.getProperty("C:\\Java\\CloseWindow.dll"));
OutputStream out = new FileOutputStream(fileOut);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
//call from jar class
Class[] params = new Class[]{ String };
Object[] parms = new Object[] { new String(title) };
URL url=new URL("jar:file:/callapi.jar/");
URLClassLoader ucl = new URLClassLoader(new URL[] { url });
Class obj = Class.forName("callapi.callapi", true, ucl);
Method m = obj.getDeclaredMethod("CloseWindow",params);
Object instance = obj.newInstance();
Object result = m.invoke(instance,parms);
}
问题是什么?String.TYPE无法正常工作
答案 0 :(得分:4)
尝试使用Class[] params = new Class[]{ String.class };
答案 1 :(得分:2)
正如其他人所指出的那样,你错过了.class
。在声明中初始化数组时,无需调用new
:
Class[] params = { String.class };
应该没问题。 (只有在以后重新初始化数组时,或者如果您创建匿名数组,则需要new
。)
事实上,您根本不需要创建params
数组。由于Class.getDeclaredMethods
是vararg method,您可以这样做
Method m = obj.getDeclaredMethod("CloseWindow", String.class);
\__________/
|
this will be transformed to a Class[] on the fly by the constructor
答案 2 :(得分:1)
尝试:
Class[] params = new Class[]{ String.class };