【问题标题】:Error: Could not find or load main class?错误:无法找到或加载主类?
【发布时间】:2015-12-05 02:10:24
【问题描述】:

我有一个名为 TestInventory.java 的类,其他类都在默认包中,都在 src 文件夹中。但是,当我运行它时,我得到:

错误:无法找到或加载主类

这是我的代码

 import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    //@ author Jane Choe
    public class TestInventory {

        public void main(String[] args) throws FileNotFoundException {
            Inventory inv = new Inventory();

            // TODO:  Read in the input file and populate the movieList. Manage exceptions.
            // On a failed movie load print the message
            // "Exception " + e.getMessage() + " for film " + title. No loading."
            FileReader file = new FileReader("movies_db.txt");
            Scanner sc = new Scanner (file);

            while (sc.hasNextLine()){
                String line = sc.nextLine();
                String [] splitline = line.split("-");
                //if (splitline[6]== null){// has as many parameters as an action movie
                    //try {// try adding to action
                System.out.println("p");
                        inv.add(new Action(splitline[0], Integer.parseInt(splitline[1]), Integer.parseInt(splitline[2]), 
                                Integer.parseInt(splitline[3]), Integer.parseInt(splitline[5])));

                    //}
                    //catch{
                    //  System.out.println("Exception + e.getMessage() + " for film " + title. No loading.");
                    //}
                //} // if loop
            }/*
            else{
                try {// try adding to RomCom
                    inv.add(RomCom((splitline[0], Integer.parseInt(splitline[1]), Integer.parseInt(splitline[2]), 
                            Integer.parseInt(splitline[3]), (splitline[4]),
                            Integer.parseInt(splitline[5]), Integer.parseInt(splitline[6]));
                }
                catch{
                    System.out.println("Exception + e.getMessage() + " for film " + title. No loading.");
                }
            }
        }
        */


        //DO NOT CHANGE
        System.out.println("Inventory should now contain file contents.");
                            System.out.println(inv.toString());

                            Movie starWars = new Action("Star Wars - A New Hope", 1977, 121, 3.8, 89);
                            inv.add(starWars);

                            Movie numberFour = new Action("I Am Number Four", 2011, 101, 3.2, 11);
                            inv.add(numberFour);

                            Movie someoneLikeYou = new RomCom("Someone Like You", 2011, 101, 3.2, 2, 5);
                            inv.add(someoneLikeYou);

                            Movie crazyStupidLove = new RomCom("Crazy, Stupid, Love 2", 2013, 113, 3.9, 1, 3);
                            inv.add(crazyStupidLove);

                            System.out.println("Inventory should now contain 13 movies. " + (inv.size() == 13));
    /* add this in later
                            if(inv.remove(crazyStupidLove.getTitle(), crazyStupidLove.getYear())) {
                                System.out.println("Successfully removed 'Crazy, Stupid, Love'");
                                if(!inv.remove(crazyStupidLove.getTitle(), crazyStupidLove.getYear())) {
                                    System.out.println("Successfully ignored second remove attempt for 'Crazy, Stupid, Love'");
                                }
                            }
    */
                            try {
                                inv.add(new RomCom("27 Dresses", 2008, 103, 4.4, 1, 1));
                            } catch (IllegalArgumentException e) {
                                System.out.println("Successfully threw exception on invalid parameter.");
                            }

                            try {
                                inv.add(someoneLikeYou);
                            } catch (MovieInventoryException e) {
                                System.out.println("Successfully threw exception on duplicate add attempt.");
                            }

                            System.out.println("Inventory should now contain 12 movies. " + (inv.size() == 12));

                            System.out.println("Inventory should not contain 'The Matrix'? " + !inv.contains("The Matrix", 1999));
                            System.out.println("Inventory should not contain 'Something Borrowed'? " + !inv.contains("Something Borrowed", 2009));
                            System.out.println("Inventory should not contain '27 Dressed'? " + !inv.contains("27 Dressed", 2008));

                            System.out.println("\n" + inv.toString());
    }

    }

我的项目旁边还有一个红色感叹号。

任何帮助将不胜感激。

【问题讨论】:

标签: java


【解决方案1】:

您忘记了 main 方法中的“静态”。

public static void main(String[] args) {
  //do something
}

【讨论】:

  • 如果此答案对您有所帮助,请考虑通过单击绿色复选标记将其标记为已接受 :)
  • @trying haha​​ 别担心,我们有时都会破坏生活中简单的事情。干杯!
猜你喜欢
  • 2016-06-12
  • 2016-03-16
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多