Java 使用反射解决引发的NoSuchMethodError异常

Java 使用反射解决引发的NoSuchMethodError异常,java,reflection,nosuchmethoderror,invocationtargetexception,Java,Reflection,Nosuchmethoderror,Invocationtargetexception,我目前正在使用Reflection在类中执行一组方法,这些类位于与我正在处理的项目不同的项目中。这些方法将依次调用此项目中的其他方法。尽管方法调用正在成功,但仍会引发由NoSuchMethodError引起的InvocationTargetException。我假设发生这种情况是因为我使用反射调用的方法调用其他方法。由于这个原因,我已经将另一个项目添加到类路径中,但这不起作用 请注意,另一个项目是一个开源项目,我从GitHub使用它进行分析,因此我不想使用它 有人能帮我吗 编辑: 以下是我当前的

我目前正在使用
Reflection
在类中执行一组方法,这些类位于与我正在处理的项目不同的项目中。这些方法将依次调用此项目中的其他方法。尽管方法调用正在成功,但仍会引发由
NoSuchMethodError
引起的
InvocationTargetException
。我假设发生这种情况是因为我使用反射调用的方法调用其他方法。由于这个原因,我已经将另一个项目添加到类路径中,但这不起作用

请注意,另一个项目是一个开源项目,我从
GitHub
使用它进行分析,因此我不想使用它

有人能帮我吗

编辑:

以下是我当前的反射代码:

   public void runSelectedTests(MethodSignature test) throws Exception{
    //no paramater
    Class<?> noparams[] = {};

    try{
        //load the test at runtime
        //get the class
        Class<?> cls = Class.forName(test.getClassName());
        Constructor<?>[] cons = cls.getDeclaredConstructors();
        //can use the first constructor if there are multiple
        //if we instantiate with all constructors you end up calling the test methods depending on
        //how many constructors you have
        Constructor<?> cons1 = cons[0];
        Object params[] = null;
        if(cons1.getParameterTypes().length > 0){
            params = new Object[cons1.getParameterTypes().length];
        }
        for(int i = 0; i < cons1.getParameterTypes().length; i++){
            String type = cons1.getParameterTypes()[i].toString();
            if(type.equals("byte") || type.equals("short") || type.equals("int")){
                params[i] = 0;
            }else if(type.equals("long")){
                params[i] = (long)0.0;
            }else if(type.equals("float")){
                params[i] = (float)0.0; 
            }else if(type.equals("double")){
                params[i] = (double)0.0;
            }else if(type.equals("char")){
                params[i] = (char)0;
            }else if(type.equals("boolean")){
                params[i] = false;
            }else{
                params[i] = null;
            }   
        }

        Object obj = cons1.newInstance(params);
        //call the test method
        Method method = cls.getDeclaredMethod(test.getName(), noparams);
        method.invoke(obj, null);
    }catch(Exception e){
        System.out.println("exception "+e.getMessage());
    }
}
编辑

这是我尝试调用的方法:

public void testCloseQuietly_AllCloseableIOException() {
    final Closeable closeable = new Closeable() {
        public void close() throws IOException {
            throw new IOException();
        }
    };
    IOUtils.closeQuietly(closeable, null, closeable);
}
错误似乎在线路上:

   IOUtils.closeQuietly(closeable, null, closeable);
类没有任何将
java.io.Closeable
作为参数的方法。它有以下几种方法:

   closeQuietly(InputStream input) 
   closeQuietly(OutputStream output)
   closeQuietly(Reader reader)
   closeQuietly(Writer writer)
你应据此通过你的论点。希望这有帮助。

类没有任何方法
closequity
java.io.Closeable
作为参数。它有以下几种方法:

   closeQuietly(InputStream input) 
   closeQuietly(OutputStream output)
   closeQuietly(Reader reader)
   closeQuietly(Writer writer)
你应据此通过你的论点。希望这有帮助。

类没有任何方法
closequity
java.io.Closeable
作为参数。它有以下几种方法:

   closeQuietly(InputStream input) 
   closeQuietly(OutputStream output)
   closeQuietly(Reader reader)
   closeQuietly(Writer writer)
你应据此通过你的论点。希望这有帮助。

类没有任何方法
closequity
java.io.Closeable
作为参数。它有以下几种方法:

   closeQuietly(InputStream input) 
   closeQuietly(OutputStream output)
   closeQuietly(Reader reader)
   closeQuietly(Writer writer)

你应据此通过你的论点。希望这有帮助。

自1.5以来,所有方法反射方法都是参数值/类型的varargs

这似乎是错误的:

method.invoke(obj, null);
如果没有参数,则这样调用它:

method.invoke(obj);
同样,这是:

Method method = cls.getDeclaredMethod(test.getName(), noparams);
可以/应该公正

Method method = cls.getDeclaredMethod(test.getName());

从1.5开始,所有方法反射方法都是参数值/类型的varargs

这似乎是错误的:

method.invoke(obj, null);
如果没有参数,则这样调用它:

method.invoke(obj);
同样,这是:

Method method = cls.getDeclaredMethod(test.getName(), noparams);
可以/应该公正

Method method = cls.getDeclaredMethod(test.getName());

从1.5开始,所有方法反射方法都是参数值/类型的varargs

这似乎是错误的:

method.invoke(obj, null);
如果没有参数,则这样调用它:

method.invoke(obj);
同样,这是:

Method method = cls.getDeclaredMethod(test.getName(), noparams);
可以/应该公正

Method method = cls.getDeclaredMethod(test.getName());

从1.5开始,所有方法反射方法都是参数值/类型的varargs

这似乎是错误的:

method.invoke(obj, null);
如果没有参数,则这样调用它:

method.invoke(obj);
同样,这是:

Method method = cls.getDeclaredMethod(test.getName(), noparams);
可以/应该公正

Method method = cls.getDeclaredMethod(test.getName());


你能用实验工作中的一些代码修改你的问题吗?这可能有助于我们提供一些答案。我刚刚为您提供了执行反射的代码。发布整个stacktrace。哪一行抛出异常<代码>方法.invoke(obj,null)?调用项目中另一个方法的行。这就是我试图将整个项目添加到类路径的原因。我的代码中没有错误。这就是为什么会抛出InvocationTargetException!你确定你调用的类有一个不带参数的同名方法吗?您正在调用哪个方法名,针对哪个类,现在该类是否声明了该方法?(你确定类在类路径上吗?你能正常调用这个方法吗?)你能用实验工作中的一些代码修改你的问题吗?这可能有助于我们提供一些答案。我刚刚为您提供了执行反射的代码。发布整个stacktrace。哪一行抛出异常<代码>方法.invoke(obj,null)?调用项目中另一个方法的行。这就是我试图将整个项目添加到类路径的原因。我的代码中没有错误。这就是为什么会抛出InvocationTargetException!你确定你调用的类有一个不带参数的同名方法吗?您正在调用哪个方法名,针对哪个类,现在该类是否声明了该方法?(你确定类在类路径上吗?你能正常调用这个方法吗?)你能用实验工作中的一些代码修改你的问题吗?这可能有助于我们提供一些答案。我刚刚为您提供了执行反射的代码。发布整个stacktrace。哪一行抛出异常<代码>方法.invoke(obj,null)?调用项目中另一个方法的行。这就是我试图将整个项目添加到类路径的原因。我的代码中没有错误。这就是为什么会抛出InvocationTargetException!你确定你调用的类有一个不带参数的同名方法吗?您正在调用哪个方法名,针对哪个类,现在该类是否声明了该方法?(你确定类在类路径上吗?你能正常调用这个方法吗?)你能用实验工作中的一些代码修改你的问题吗?这可能有助于我们提供一些答案。我刚刚为您提供了执行反射的代码。发布整个stacktrace。哪一行抛出异常<代码>方法.invoke(obj,null)?调用项目中另一个方法的行。这就是我试图将整个项目添加到类路径的原因。我的代码中没有错误。这就是为什么会抛出InvocationTargetException!你确定你调用的类有一个不带参数的同名方法吗?您正在调用哪个方法名,针对哪个类,现在该类是否声明了该方法?(你确定这个类在类路径上吗?你能正常调用这个方法吗?)我有从GitHub下载的源代码。我很确定有一个方法是以java.io.Closeable作为参数的。它在IOUtils.java中