【发布时间】:2013-07-27 22:11:56
【问题描述】:
我是 Java 中 ClassLoader 问题的新手。那么我怎样才能调用像
这样的方法getDefault().GetImage();
这是我当前的代码:
ClassLoader tCLSLoader = new URLClassLoader(tListURL);
Class<?> tCLS = tCLSLoader.loadClass("com.github.sarxos.webcam.Webcam");
// MY FAILED TEST
Method tMethod = tCLS.getDeclaredMethod("getDefault().GetImage");
tMethod.invoke(tCLS, (Object[]) null);
编辑:
我试过这个:
Method tMethod1 = tCLS.getDeclaredMethod("getDefault");
Object tWebCam = tMethod1.invoke(tCLS, (Object[]) null);
// WebCam - Class
Class<?> tWCClass = tWebCam.getClass();
Method tMethod2 = tWCClass.getDeclaredMethod("getImage");
tMethod2.invoke(tWCClass, (Object[]) null);
但我明白了:
java.lang.IllegalArgumentException: object is not an instance of declaring class
我需要得到这个结果:
BufferedImage tBuffImage = Webcam.getDefault().getImage();
【问题讨论】:
-
呃,有趣。不完全是 Java 的工作原理。你到底想做什么?你当然不能这样做。你需要知道
getDefault()的返回类型是什么,强制转换然后调用next方法。 -
再看帖子,我修改了。谢谢!
-
你需要传入实例而不是
Class所以tMethod2.invoke(tWebCam, (Object[]) null); -
我明白了!!!!非常感谢!!! :)
标签: java classloader getmethod