启动程序时出现“Main method not found”错误?

我正在学习Java课程,而且我碰到了一堵砖墙。 我一直负责开发一个简单的命令行程序。 为了方便起见,我给了下面的示例代码来修改,所以我不必从头开始。

package assignment; public class Main { private final static String[] mainMenuOpts = {"Students","Lecturers","Admin","Exit"}; private final static String[] studentMenuOpts = {"Add Student","List all Students","Find a Student","Return to Main Menu"}; private Menu mainMenu = new Menu("MAIN MENU",mainMenuOpts); private Menu studentMenu = new Menu("STUDENT MENU",studentMenuOpts); private DataStore data = new DataStore(); private java.io.PrintStream out = System.out; private ReadKb reader = new ReadKb(); /** Creates a new instance of Main */ public Main() { run(); } private void run(){ int ret = mainMenu.display(); while(true){ switch(ret){ case 1: students();break; case 2: lecturers(); break; case 3: admin(); break; case 4: exit(); break; } ret = mainMenu.display(); } } private void students(){ int ret = studentMenu.display(); while(ret != 4){ switch(ret){ case 1: addStudent();break; case 2: listStudents(); break; case 3: findStudent(); break; } ret = studentMenu.display(); } } private void lecturers(){ out.println("\nLecturers not yet implemented"); } private void admin(){ out.println("\nAdmin not yet implemented"); } //Student methods private void addStudent(){ out.println("\n\tAdd New Student"); //prompt for details //add student to the datastore //ask if they want to enter another student - // if so call addStudent again //otherwise the method completes and the studentMenu will display again } private void listStudents(){ out.println("\n\tStudent Listing"); //list all students from the datastore } private void findStudent(){ out.println("\n\tFind Student"); out.print("Enter Search String: "); //reasd search text //use datastore method to get list of students that contain the search string //display matching students } // end Student methods private void exit() { data.save(); //call the datastore method that will save to file out.println("\n\nGoodbye :)"); System.exit(0); } } 

我正在使用NetBeans,当我尝试运行该项目时出现此错误:

 Error: Main method not found in class assignment.Main, please define the main method as: public static void main(String[] args) 

我只是想让程序运行,所以我可以更好地理解代码。 我明白这个错误,但是不知道在这个文本的墙上实现主要方法的位置。 我一直在尝试几个小时,但显然作为一个新手我完全没用。 任何帮助将不胜感激。

你目前所拥有的仅仅是一个名为Main的构造函数,Java所需要的是一个具有如下精确签名的主要方法:

 public static void main(String[] args) 
  • public – 所以它可以从外面调用

  • static – 这样就不需要创build你的类的实例

  • void – 不会返回任何值

  • args – 用于在运行程序时可以指定的命令行参数的数组

这是您的应用程序的入口点。

当你正在调用当前的代码时,JVM正在尝试定位main方法,并且由于它不存在于你的代码中,所以它抛出了你收到的exception。

既然你已经在你的post中提到过初学者,值得一提的是Java是一个区分大小写的语言mainMain在Java中是不一样的。

另请参阅: 入门教程 。

main的正确签名是:

 public static void main(String[] args) { new Main(); } 

它甚至写在您发布的错误消息。

删除; 从构造函数:

 public Main() { run(); } 

你必须在程序中使用main()方法。 从这里开始执行程序。

喜欢

 public static void main(String args[]) { //This is the starting point of your program. } 

这个方法必须出现在一个类中,但它可以是任何类。 在Java语言中,当您使用Java解释器执行类时,运行时系统将首先调用该类的main()方法。 main()方法然后调用运行应用程序所需的所有其他方法。

main()方法接受一个参数:一个string数组。 此参数是运行时系统将命令行parameter passing给应用程序的机制

它正在寻找这个签名的方法:

 public static void main(String[] args) 

要运行你的代码, main方法可以像这样:

 public static void main(String[] args) { new Main(); } 

正如相当有用的错误信息所述,您需要一个主要方法。 看到的教程 。

main方法应该存在为您的应用程序运行。 Java应用程序需要它来知道从哪里开始执行程序。

把方法放在你select的类中,然后右键单击文件并select“运行文件”。

  public static void main(String[] args) { // your code here } 

您需要在主类中添加主方法,以便JVM知道从哪里开始,而不是使用“主”名称的类。

 public static void main(String[] args) { new Main(); }