简体   繁体   English

为什么在尝试调用main时Class#getDeclaredMethods0失败?

[英]Why does Class#getDeclaredMethods0 fail when trying to invoke main?

Why does this code snippet fail with IllegalArgumentException: wrong number of arguments ? 为什么此代码片段会因IllegalArgumentException: wrong number of arguments失败IllegalArgumentException: wrong number of arguments Demonstrated code works with ordinary member and static methods, as well as with native declared ones... 演示的代码可与普通成员和静态方法以及本机声明的方法一起使用...

   public class Program {
        public static void main(String[] args) throws ReflectiveOperationException {
            //get native getDeclaredMethods method
            Method Class$getDeclaredMethods0 = Class.class.getDeclaredMethod("getDeclaredMethods0", boolean.class);
            Class$getDeclaredMethods0.setAccessible(true);

            //call main
            Method[] methods = (Method[]) Class$getDeclaredMethods0.invoke(Program.class, false);
            Method main = methods[0];
            main.invoke(null, args); // ← Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
            //at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            //at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            //at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            //at java.lang.reflect.Method.invoke(Method.java:606)
            //at Program.main(Program.java:19)
            //at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            //at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            //at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            //at java.lang.reflect.Method.invoke(Method.java:606)
            //at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
        }
    }

It's not clear why you're calling getDeclaredMethods via reflection, but the invocation of main is broken because you're trying to call it as if it had several String parameters - one per value in args . 目前尚不清楚为什么要通过反射调用getDeclaredMethods ,但是对main的调用已中断,因为您试图像具有多个String参数一样对其进行调用-每个args值一个。 Instead, you want to pass a single argument, of type String[] . 相反,你想传递一个参数,类型String[] You can do that like this: 您可以这样做:

main.invoke(null, new Object[] { args });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Class类中的getDeclaredMethods() - getDeclaredMethods() in Class class 为什么mediaRecorder在尝试录制语音时无法准备 - why does mediaRecorder fail to prepare when trying to record voice 反射getDeclaredMethods()和不在类路径中的类 - Reflection getDeclaredMethods() and class that is not in classpath 为什么ForkJoinPool :: invoke()会阻塞主线程? - Why does ForkJoinPool::invoke() block the main thread? 为什么.class不会调用类中的静态块? - Why does .class not invoke the static block in a Class? 添加到jruby类的getDeclaredMethods() - adding to a jruby class's getDeclaredMethods() 为什么调用join方法时主线程消失了? - why the main thread is gone when invoke join method? 当我的班级有一个主要对象时,为什么会得到“选择不包含主要类型”的信息? - Why do I get the “Selection does not contain a main type” when my class has a main? 为什么SBT无法在Java中检测主类? (未检测到主班) - Why does SBT not detect main class in Java? (No main class detected) 为什么在编译时不能注入injects子句中的类时dagger不会失败? - Why does dagger not fail at compile-time when a class in the injects clause cannot be injected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM