English 中文(简体)
启动程序时“ 找不到主方法” 错误? [重复]
原标题:"Main method not found" error when starting program? [duplicate]
  • 时间:2012-05-22 16:08:55
  •  标签:
  • 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("
Lecturers not yet implemented");
}
private void admin(){
    out.println("
Admin not yet implemented");
}
//Student methods
private void addStudent(){
    out.println("
	Add 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("
	Student Listing");
    //list all students from the datastore
}
private void findStudent(){
    out.println("
	Find 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("

Goodbye :)");
    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 - 以便从外部调用

  • stistati - 因此不需要创建您的类实例

  • evue - 不返回任何值

  • args - 您在运行程序时可以指定的命令行参数矩阵

这是您申请的切入点 。

当您当前代码被引用时, JVM 正在试图找到主方法, 由于它不在您的代码中, 它会丢弃您收到的例外 。

既然您在文章中提到初学者, 值得一提的是, Java 是一种“强势”的敏感语言 main 和 Main 在 Java 中是不一样的。

另见:开始辅导

main的正确签名是:

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

它甚至写在你张贴的错误信息中。

从构建器中删除 ; :

public Main() {
    run();
}

You have to use main() method in your program. From here the program execution starts.

类似

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

This method must appear within a class, but it can be any class. In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class s main() method. The main() method then calls all the other methods required to run your application.

主 () 方法接受一个单一参数: 字符串数组。 此参数是运行时系统将命令行参数传送到您应用程序的机制

它正在寻找一种方法 使用这种签名:

public static void main(String[] args)

要运行您的代码, main 方法可以看起来像这样 :

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

正如非常有用的错误信息所说明的,您需要一种主要的方法。 见java教程

main 方法应该存在,才能运行您的应用程序。 Java 应用程序需要知道在哪里开始执行程序。

在您选择的类别中放置该方法, 然后右键单击文件并选择运行文件 。

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

您需要在主类中添加一种主要方法, 以便 JVM 能够知道从何处开始, 而不是有“ main” 名称的类 。

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签