【问题标题】:java.lang.NoSuchMethodError: main [duplicate]java.lang.NoSuchMethodError:主要[重复]
【发布时间】:2011-07-21 09:09:59
【问题描述】:

可能重复:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

我收到以下错误:

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

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class SwimCalc extends JFrame implements ActionListener {
   private JTabbedPane jtabbedPane;
   private JPanel Customers;

   JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors,
         ExistTextContractors;

   public SwimCalc() {
      setTitle("Volume Calculator");
      setSize(300, 200);

      JPanel topPanel = new JPanel();
      topPanel.setLayout(new BorderLayout());
      getContentPane().add(topPanel);

      createCustomers();

      jtabbedPane = new JTabbedPane();
      jtabbedPane.addTab("Customers", Customers);
      topPanel.add(jtabbedPane, BorderLayout.CENTER);
   }

   /* CREATE CUSTOMERS */

   public JPanel createCustomers() {
      Customers = new JPanel();
      Customers.setLayout(null);

      NameTextCustomers = new JTextArea();
      NameTextCustomers.setBounds(10, 10, 350, 150);
      NameTextCustomers.setLineWrap(true);
      Customers.add(NameTextCustomers);

      JButton Exit = new JButton("Exit");
      Exit.setBounds(30, 170, 80, 20);
      Exit.addActionListener(this);
      Exit.setBackground(Color.white);
      Customers.add(Exit);

      JButton AddCustomers = new JButton("Add Customer");
      AddCustomers.setBounds(130, 170, 120, 20);
      AddCustomers.setBackground(Color.white);
      Customers.add(AddCustomers);

      JButton Refresh = new JButton("Refresh");
      Refresh.setBounds(260, 170, 80, 20);
      Refresh.setBackground(Color.white);
      Customers.add(Refresh);

      ExistTextCustomers = new JTextArea();
      ExistTextCustomers.setBounds(10, 200, 350, 60);
      ExistTextCustomers.setLineWrap(true);
      Customers.add(ExistTextCustomers);

      final JTextArea custArea = new JTextArea(6, 30);
      final JTextArea custMessage = null;

      AddCustomers.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            new Customer("Customer");
         }
      });
      Customers.add(custArea);
      Customers.add(AddCustomers);

      Customers.add(Refresh);
      Customers.add(custMessage);
      Refresh.setMnemonic('R');

      Refresh.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            custMessage.setText("");
            try {
               File custOpen = new File("customer.txt");
               FileReader custAreaIn = new FileReader(custOpen);
               custArea.read(custAreaIn, custOpen.toString());
               custMessage.setText("The file exists and can be read from.");
            } catch (IOException e3) {
               custMessage.setText("The file could not be read. "
                     + e3.getMessage());
            }
         }
      });
      return Customers;
   }

   class Customer extends JFrame {
      private String[] states = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT",
            "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA",
            "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
            "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
            "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" };
      private JComboBox StateList = new JComboBox(states);
      private JTextField NameText = new JTextField(25);
      private JTextField AddressText = new JTextField(25);
      private JTextField CityText = new JTextField(25);
      private JTextField ZipText = new JTextField(9);
      private JTextField PhoneText = new JTextField(10);
      private JTextField PopMessageText = new JTextField(30);
      private static final long serialVersionUID = 1L;

      private AddCustButtonHandler addCusHandler = new AddCustButtonHandler();

      public Customer(String who) {
         popUpWindow(who);
      }

      public void popUpWindow(final String who) {

         final JFrame popWindow;
         popWindow = new JFrame(who);
         popWindow.setSize(425, 350);
         popWindow.setLocation(100, 100);
         popWindow.setVisible(true);
         setDefaultCloseOperation(EXIT_ON_CLOSE);

         Container c = new Container();

         popWindow.add(c);

         c.setLayout(new FlowLayout());

         JPanel one = new JPanel();
         JPanel two = new JPanel();
         JPanel three = new JPanel();
         JPanel four = new JPanel();
         JPanel five = new JPanel();
         JPanel six = new JPanel();

         one.add(new JLabel(who + " Name "));
         one.add(NameText);
         two.add(new JLabel("Address "));
         two.add(AddressText);
         three.add(new JLabel("City "));
         three.add(CityText);
         four.add(new JLabel("State "));
         StateList.setSelectedIndex(0);
         four.add(StateList);
         four.add(new JLabel("ZIP"));
         four.add(ZipText);
         four.add(new JLabel("Phone"));
         four.add(PhoneText);
         JButton addwho = new JButton("Add " + who);
         addwho.setMnemonic('A');
         JButton close = new JButton("Close");
         close.setMnemonic('C');
         JButton deleteFile = new JButton("Delete File");
         deleteFile.setMnemonic('D');
         five.add(addwho);
         five.add(close);
         five.add(deleteFile);
         PopMessageText.setEditable(false);
         PopMessageText.setHorizontalAlignment(JTextField.CENTER);

         six.add(PopMessageText);
         c.add(one);
         c.add(two);
         c.add(three);
         c.add(four);
         c.add(five);
         c.add(six);

         deleteFile.setToolTipText("Delete File");
         addwho.setToolTipText("Add " + who);
         close.setToolTipText("Close");

         if (who == "Customer")
            addwho.addActionListener(addCusHandler);
         close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               NameText.setText("");
               AddressText.setText("");
               CityText.setText("");
               ZipText.setText("");
               PhoneText.setText("");
               PopMessageText.setText("");
               popWindow.dispose();
            }
         });
         deleteFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               PopMessageText.setText("");
               if (who == "Customer") {
                  File file = new File("Customer.txt");
                  boolean cusFileDeleted = file.delete();
                  if (cusFileDeleted) {
                     PopMessageText.setText("Customer file has been deleted");
                  } else {
                     PopMessageText
                           .setText("There was an erron in deleting file");
                  }
               }
            }
         });
      }

      class AddCustButtonHandler implements ActionListener {
         public void actionPerformed(ActionEvent addCusHandler) {
            int StateIndex;
            try {
               File file = new File("Customer.txt");

               boolean success = file.createNewFile();

               if (success) {
                  PopMessageText
                        .setText("Customer.txt file created file added");
               } else if (file.canWrite()) {
                  PopMessageText
                        .setText("Writing data to Customer.txt, file added");
               } else {
                  PopMessageText.setText("Cannot create file: Customer.txt");
               }
               try {
                  FileWriter fileW = new FileWriter("Customer.txt", true);
                  fileW.write(NameText.getText());
                  fileW.write(",");
                  fileW.write(AddressText.getText());
                  fileW.write(",");
                  fileW.write(CityText.getText());
                  fileW.write(",");
                  StateIndex = StateList.getSelectedIndex();
                  fileW.write(states[StateIndex]);
                  fileW.write(",");
                  fileW.write(ZipText.getText());
                  fileW.write(",");
                  fileW.write(PhoneText.getText());
                  fileW.write("\r\n");
                  fileW.close();
                  PopMessageText.setText("A new Customer has been added!");

                  FileReader fileR = new FileReader("Customer.txt");
                  BufferedReader buffIn = new BufferedReader(fileR);

                  String textData = buffIn.readLine();
                  buffIn.close();
               } catch (IOException e1) {
                  JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR",
                        2);
               }
               NameText.setText("");
               AddressText.setText("");
               CityText.setText("");
               ZipText.setText("");
               PhoneText.setText("");
            } catch (IOException e1) {
            }

         }
      }

      public void actionPerformed(ActionEvent event) {
      }

      private void Exit_pressed() {
         System.exit(0);
      }

      public void main(String[] args) {
         JFrame frame = new SwimCalc();
         frame.setSize(380, 350);
         frame.setVisible(true);
      }
   }

   public void actionPerformed(ActionEvent e) {

   }
}

错误:

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

public void actionPerformed(ActionEvent e) {

    }
    public static void main(String[] args){
        JFrame frame = new SwimCalc();
        frame.setSize(380, 350);
        frame.setVisible(true);
        }
    }

【问题讨论】:

  • 根本不是那个问题的副本。像这样极其常见的错误消息可能有不同的原因,并且将新手指向一个解决完全不同原因的问题并不是很有建设性的。
  • 我上面复制的所有代码都是我正在运行的。当我将它转换为静态时,我收到此错误:线程“main”java.lang.Error 中的异常:未解决的编译问题:方法 main 不能被声明为静态;静态方法只能在 SwimCalc$Customer.main(SwimCalc.java:267) 处以静态或顶级类型声明
  • @Mike:见my answer
  • @Mark:我把它和最后一个括号一起移到了,如果需要的话,仍然没有运气。
  • 查看stackoverflow.com/questions/5407250/… 以获得通用答案

标签: java eclipse


【解决方案1】:

main 需要是一个静态方法。

public static void main(String[] args){
   JFrame frame = new SwimCalc();
   frame.setSize(380, 350);
   frame.setVisible(true);
}

另外,main 方法应该在SwimCalc 类或其他一些顶级类中。将方法移动到 SwimCalc 类中(不在您的 Customer 类中)并使用 java SwimCalc 调用它。不能在内部类中声明 main

【讨论】:

    【解决方案2】:
    public **static** void main(String[] args){
    

    【讨论】:

      【解决方案3】:

      看起来你的main 方法实际上是你的actionPerformed 方法。你显然不能这样做。声明需要在您的 SwimCalc 类中。那是你试图运行的实际代码的疯狂吗?你到处都是大括号和杂散的括号,没有缩进;如果你只是清理你的代码,问题会更容易找到。

      public void main(String[] args){
      

      应该是

      public static void main(String[] args){
      

      还有这样的废话:} );。你有这么多语法错误...

      【讨论】:

      • 哈哈,是的,我也是这么想的,直到我发现一个右大括号一直隐藏在代码区域的右侧。我更新了问题以正确格式化代码。
      • 哦,干得好。是的,我没有能够做到这一点的 mod 点
      【解决方案4】:

      main方法必须是public static

      public static void main(String[] args)
      

      【讨论】:

        【解决方案5】:

        main() 必须是静态的。完整的签名是:

        public static void main(String[] args)
        

        【讨论】:

        • 之后我遇到了同样的错误。将鼠标悬停在错误上时的选项之一是“将静态修饰符添加到父类型”。我尝试这样做,但出现了与所述相同的错误。
        • @Mike:您当前的代码无法编译并且格式不正确。你能给我们真正的代码,格式干净吗?
        • @Mark:我知道它不能编译。我还在学习java(非常新),所以我知道它不会达到专业标准。当您运行它时,它会给出我所说的错误吗?
        • @Mike:如果没有“编译”,就没有“运行”。您的类必须编译后才能运行。但是,您可以在我的回答中看到您可能遇到的另一个问题的更新。
        • 其实可以编译,我错了。格式非常奇怪,以至于显示区域中的一些大括号被裁剪掉了。
        【解决方案6】:

        应该是

        public static void main(String[] args){
        

        而不是public void main...

        【讨论】:

          【解决方案7】:

          您的主要方法必须是静态的。这应该是签名:

          public static void main(String[] args)
          

          【讨论】:

            【解决方案8】:

            是的,签名应该是

            public static void main(String[] args)
            

            即使 args 丢失,eclipse 也会抛出这个错误。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-20
              • 1970-01-01
              • 2017-08-28
              • 1970-01-01
              • 2011-06-17
              • 2018-06-22
              • 2015-03-24
              • 2012-08-12
              相关资源
              最近更新 更多