我正在java应用程序的运行时创建一个类(名为haan.java)。 此类的对象插入Oracle Coherence Cache中 当我从缓存中获取一个对象(tempHaan)时,它是haan类型。但是当我尝试调用此对象的方法(tempHaan)时,我收到错误" java.lang.IllegalArgumentException:object不是声明类的实例"
错误发生在"对象invoke = methodgetHash.invoke(tempHaan,null);"
$(document).ready(function(){
$('#hello').click(function(){
var name = $("#voucher").val();
alert(name);
});
});
答案 0 :(得分:1)
您动态创建ULRClassLoader
来加载类,即缓存实例的类和您从中调用的方法来自不同类加载器的类,从而导致此错误。您可以从要缓存的实例获取声明的方法并将其与实例一起缓存,或者从缓存中检索实例并通过调用
Method methodgetHash = tempHaan.getDeclaredMethod("getHash", noparams);
将方法与要缓存的类一起缓存将带来性能改进BTW。
答案 1 :(得分:0)
对我有用的代码:
ClassLoader currentThreadClassLoader = Thread.currentThread()。getContextClassLoader();
URLClassLoader classLoader =(URLClassLoader)currentThreadClassLoader.getSystemClassLoader();
cls = URLClassLoader.class;
方法m = cls.getDeclaredMethod(" addURL",new Class [] {URL.class});
m.setAccessible(真);
URL ur = f.toURI()。toURL();
m.invoke(classLoader,new Object [] {ur});
cls = Class.forName(rtomProperties.getPropertyValue(" pojoPackageLocation")。concat(" .haan"),
true,currentThreadClassLoader);