“未找到主要方法”启动程序时出错? [英] "Main method not found" error when starting program?

查看:67
本文介绍了“未找到主要方法”启动程序时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning Java for my course and I've hit a brick wall. I've been tasked with developing a simple command line program. To make things easier I was given the following sample code to modify so I wouldn't have to start from scratch.

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,当我尝试运行时该项目我收到此错误:

I'm using NetBeans and when I try to run the project I get this error:

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

我只想让程序运行所以我可以更好地理解代码。我理解错误,但不知道在这个文本墙中实现main方法的位置。我已经做了几个小时的实验,但很明显,作为一个新手,我完全没用。任何帮助将不胜感激。

I just want to get the program running so I can understand the code better. I understand the error, but have no idea where to implement the main method in this wall of text. I've been experimenting for hours, but obviously as a newbie I'm completely useless. Any help would be greatly appreciated.

推荐答案

您目前所拥有的只是一个名为Main的构造函数,Java需要的是一个主要方法准确签名为:

What you have currently is just a constructor named Main, what Java needs is a main method with exact signature as:

public static void main(String[] args)




  • public - 以便可以从中调用外部

    • public - so that it can be called from outside

      静态 - 这样就不需要创建类的实例

      static - so that no need to create an instance of your class

      void - 不会返回任何值

      void - not going to return any value

      args - 运行程序时可以指定的命令行参数数组

      args - an array for command line parameters that you can specify while running the program

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

      This is the entry point for your application.

      当您调用当前代码时,JVM正在尝试定位main方法,它没有出现在你的代码中,它会抛出你收到的异常。

      When your current code is being invoked, JVM is trying to locate main method, and since its not present in your code, it's throwing the exception which you have received.

      既然你在哟中提到了初学者你的帖子,值得一提的是Java是区分大小写的语言 - main Main 在Java中不一样。

      Since you have mentioned beginner in your post, its worth mentioning that Java is a case sensitive language - main and Main are not same in Java.

      另请参阅:入门教程

      这篇关于“未找到主要方法”启动程序时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆