【问题标题】:Error: Main method not found in class MovieDatabase [duplicate]错误:在类 MovieDatabase 中找不到主要方法 [重复]
【发布时间】:2015-04-20 10:05:31
【问题描述】:

错误:在类 MovieDatabase 中找不到主要方法,请将主要方法定义为: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 %2$10s %3$10s %4$10s ", "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 %2$10s %3$10s %4$10s ", j, analysis[j], "", "");
                }
            }   
        }
    }
} 

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

【问题讨论】:

    标签: java methods main


    【解决方案1】:

    这是因为你忘了添加 main 方法。错误清楚地表明:

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

    所以添加主:

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

    【讨论】:

      【解决方案2】:

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

      public static void main(String args[])
      

      public - 所有人都应该可以访问它
      静态 - JVM 无法创建您的类的实例,因此方法应该是 static
      void - 方法不返回任何东西

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

      【讨论】:

        【解决方案3】:

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

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

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

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

        public static void main(String[] args)
        

        【讨论】:

          【解决方案4】:

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

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

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

          public class MovieDatabase extends Application
          

          【讨论】:

            猜你喜欢
            • 2013-06-25
            • 2016-03-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-22
            • 1970-01-01
            • 2014-12-05
            • 1970-01-01
            相关资源
            最近更新 更多