Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getDeclaredMethod不断给出NoSuchMethodException_Java_Nosuchmethoderror - Fatal编程技术网

Java getDeclaredMethod不断给出NoSuchMethodException

Java getDeclaredMethod不断给出NoSuchMethodException,java,nosuchmethoderror,Java,Nosuchmethoderror,我看过一些关于getDeclaredMethod的NoSuchMethodException的帖子,但我仍然无法解决这个问题 我把这个问题简单地说了出来,有人能让它工作吗: public class MainMethodTest { public static void main(String[] args) { try { //Method m = MainMethodTest.class.getDeclaredMethod("main");

我看过一些关于getDeclaredMethod的NoSuchMethodException的帖子,但我仍然无法解决这个问题

我把这个问题简单地说了出来,有人能让它工作吗:

public class MainMethodTest {

    public static void main(String[] args) {

        try {
            //Method m = MainMethodTest.class.getDeclaredMethod("main");
            Method m = MainMethodTest.class.getDeclaredMethod("main", MainMethodTest.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

}
正在尝试查找代码中没有的
main(MainMethodTest参数)
方法

如果要获取
main(字符串[]参数)
方法,请使用

Method m = MainMethodTest.class.getDeclaredMethod("main", String[].class);

方法
getDeclaredMethod
的第二个参数是要获取的方法的参数。在你的情况下,它是主要的。因此,必须指定arg参数

    Class[] argTypes = new Class[] { String[].class };
    Method main = MainMethodTest.class.getDeclaredMethod("main", argTypes); 

检查如何调用main方法。

您可能希望了解的第二个参数的含义。提示:您的
main
方法没有类型为
MainMethodTest
的参数。。。。它也没有参数(适用于您的注释行)。用
getDeclaredMethod(“main”,String[].class)
试试。啊,很酷,我以前用
Sting.class
试过,但没有用括号。
    Class[] argTypes = new Class[] { String[].class };
    Method main = MainMethodTest.class.getDeclaredMethod("main", argTypes);