Java 错误:在类 MovieDatabase 中找不到 Main 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28599985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:29:26  来源:igfitidea点击:

Error: Main method not found in class MovieDatabase

javamethodsmain

提问by Michelle

Error: Main method not found in class MovieDatabase, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

错误:在类 MovieDatabase 中找不到 Main 方法,请将主要方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.Arrays;

public class MovieDatabase {
    private int[] analysis;

    //creating the contructor
    public MovieDatabase(String file){
        analysis = new int[2015];

        this.load(file);
    }
        //uses the load(String file) method from downstairs to do all of the work


    public void  load(String file){
        Scanner theScanner = null;

        try{
            //inputing the into the scanner
            theScanner = new Scanner(new FileInputStream(file));
        }
        catch(Exception ex){ 
            ex.printStackTrace();
        }
        // as long as the scanner has another line 
        while(theScanner.hasNextLine())
        {
            String Line = theScanner.nextLine();
            //make an array called split and allocated different elements based on a seperation of ##
            String split[] = Line.split("##");
            int year = Integer.valueOf(split[1]); 
            analysis[year] ++;
        }   

    }

    //print out the array in the synchronous format
    public void print(){
        System.out.printf("%1$-30s %2s %3s %4s ", "Year", "Occurances", "", "");
        //go through the array
        for (int i =0;i < analysis.length ;i++ ) {
            if(analysis[i] >0){
                for (int j =i;j < analysis.length ;i++ ){
                    System.out.printf("%1$-30s %2s %3s %4s ", j, analysis[j], "", "");
                }
            }   
        }
    }
} 

How do I fix this error message? Ive read other similar questions but just say to make the classes public. Mine are public.

如何修复此错误消息?我读过其他类似的问题,但只是说要公开课程。我的是公开的。

回答by SMA

As the error suggests, if its non FX project, just define something like:

正如错误所暗示的那样,如果它的非 FX 项目,只需定义如下内容:

public static void main(String args[]) {
    ...
}

Or else change you class definition so it extends Application, something like:

或者更改您的类定义,使其扩展应用程序,例如:

public class MovieDatabase extends Application

回答by Abimaran Kugathasan

main()method in Java is an standard method which is used by JVM to start execution of any Java program. main method is referred as entry point of Java application which is true in case of core java application

main()Java 中的方法是一种标准方法,JVM 使用它来启动任何 Java 程序的执行。main 方法被称为 Java 应用程序的入口点,这在核心 Java 应用程序的情况下是正确的

You have missed it. Add following main()method

你已经错过了。添加以下main()方法

public static void main(String[] args) {
    MovieDatabase db = new MovieDatabase("file/path/goes/here");
    db.print();
}

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

在 Java 编程语言中,每个应用程序都必须包含一个 main 方法,其签名为:

public static void main(String[] args)

回答by atish shimpi

To invoke any application JVM need main()method, and which should have following access specifiers and modifiers,

要调用任何应用程序 JVM 需要的main()方法,并且应该具有以下访问说明符和修饰符,

public static void main(String args[])

public- It should be accessible to all
static- JVM not able to create instance of your class so method should be static
void- method not returning anything

public- 它应该可供所有
静态访问- JVM 无法创建您的类的实例,因此方法应该是static
无效的- 方法不返回任何内容

For each java application main method is entry point, so your application should have atleast one main method to start execution of application.

对于每个 java 应用程序,main 方法都是入口点,因此您的应用程序应该至少有一个 main 方法来启动应用程序的执行。

回答by Leo

It's because you have forgot to add the main method. The error clearly says:

那是因为你忘记添加main方法了。错误明确指出:

please define the main method as: public static void main(String[] args)

请将main方法定义为:public static void main(String[] args)

So add the main:

所以添加主要:

public static void main(String[] args) {
    MovieDatabase m = new MovieDatabase("Your File Path");
    m.print();
}