Search code examples
javaprogram-entry-pointnosuchmethoderror

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


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

Exception in thread "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);  
    }
}

Solution

  • Your main method is not static, and you should make it static. Check this to see why

    public static void main(String [] args)