主要方法未找到错误Java

时间:2015-10-07 21:04:05

标签: java eclipse

我正在尝试在Eclipse中运行下面的简单字数统计程序,我收到错误。我在运行方式中检查了我的配置并且它们是正确的

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

这是程序

包wordcount;

import java.io.*;
import java.util.*;

// This program does word-counting on the text of Moby Dick.
public class WordCount {
  public static void main(String[] args) throws FileNotFoundException {
    HashMap map = new HashMap();  // word --> # of occurrences

    // read each word from the file
    Scanner in = new Scanner(new File("mobydick.txt"));
    while (in.hasNext()) {
      String word = in.next();

      if(map.containsKey(word)) {
        // if we have already seen this word before,
        // increment its count by one
        Integer count = (Integer)map.get(word);
        map.put(word, new Integer(count.intValue() + 1));
      } else {
        // we haven't seen this word, so add it with count of 1
        map.put(word, new Integer(1));
      }
    }

    // now print out every word in the book, along with its count,
    // in alphabetical order
    ArrayList arraylist = new ArrayList(map.keySet());
    Collections.sort(arraylist);

    for (int i = 0; i < arraylist.size(); i++) {
      String key = (String)arraylist.get(i);
      Integer count = (Integer)map.get(key);
      System.out.println(key + " --> " + count);
    }

  }
}

2 个答案:

答案 0 :(得分:0)

为什么你的main方法会抛出异常?你认为你在哪里投掷它?你的主要方法是处理它的最后机会。

你的代码运行得非常顺利。你确定你保存了代码吗?如果没有,代码就不会被编译,因此无法找到该方法。

答案 1 :(得分:0)

当您在IDE中按下RUN按钮时,它会尝试运行上次使用的项目/类,因此在单击RUN后可能不会启动此类。附:抱歉英文不好。