我如何解决线程“main”中的异常 java.lang.NoSuchMethodError: main

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17783812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:58:52  来源:igfitidea点击:

How can i resolve the Exception in thread "main" java.lang.NoSuchMethodError: main

javamainnosuchmethoderror

提问by user2592584

I have this code. Eclipse tells me that the syntax is correct but when I run the program it gives me this error:

我有这个代码。Eclipse 告诉我语法是正确的,但是当我运行程序时,它给了我这个错误:

Exception in thread "main" java.lang.NoSuchMethodError: main

线程“main”中的异常 java.lang.NoSuchMethodError: main

What's wrong?

怎么了?

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;
    public void main(String[] args){
        JFrame Main = new JFrame("TEST");
        Main.setSize(600, 600);
        Main.setLocationRelativeTo(null);
        Main.setVisible(true);
        Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Adding JPanel     
        JPanel panel = new JPanel();
        Main.add(panel);
//JPanel settings
        panel.setLayout(null);
        panel.setBackground(Color.GREEN);
//Adding JButton
        JButton button = new JButton("Button 1");
        JButton button2 = new JButton("Button2");
        panel.add(button);
        panel.add(button2);   
//Button action
        button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        Main.this.getContentPane().remove(panel);
        Main.this.getContentPane().add(panel2);
        Main.this.getContentPane().validate();      
    }
});

//JButton settings
        button.setBounds(70, 160, 200, 200);
        button2.setBounds(320, 160, 200, 200);  
    }
}

回答by sanbhat

Your main method is notstatic, and you should make it static. Check thisto see why

您的主要方法不是static,您应该使用它static检查这个看看为什么

public static void main(String [] args)

回答by Suresh Atta

Your main method should be static

你的主要方法应该是 static

  public  static  void main(String[] args){
  ----

}

And see why ??Why is the Java main method static?

看看为什么?为什么 Java 的 main 方法是静态的?

回答by Andrew Thompson

The class requires a method with a signature of:

该类需要一个具有以下签名的方法:

public static void main(String[])

回答by Svend Hansen

You have to make your main method static:

你必须使你的主要方法静态:

public static void main(String[] args) {

}

回答by newuser

public static void main(String[] args)

instead of

代替

public void main(String[] args)

publicmeans that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.

public意味着该方法是可见的,可以从其他类型的其他对象调用。其他替代方案是私有的、受保护的、包和包私有的。请参阅此处了解更多详情。

staticmeans that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

static意味着该方法与类相关联,而不是该类的特定实例(对象)。这意味着您可以在不创建类的对象的情况下调用静态方法。

voidmeans that the method has no return value. If the method returned an int you would write int instead of void.

void意味着该方法没有返回值。如果该方法返回一个 int,您将编写 int 而不是 void。

The combination of all three of these is most commonly seen on the main method which most tutorials will include.

所有这三个的组合在大多数教程将包含的主要方法中最常见。

回答by gyorgyabraham

Your main method must be static (ie. class-bound not instance-bound) because at the time of starting your app there no class instances that could call your class' main method.

您的 main 方法必须是静态的(即类绑定而不是实例绑定),因为在启动应用程序时,没有可以调用类的 main 方法的类实例。

回答by Manish Doshi

The class requires a method with a signature of like this:

该类需要一个具有如下签名的方法:

public static void main(String[])