-2

我正在跟随斯坦福的 CS106a 课程并尝试做作业。我在运行书中的示例代码时遇到了困难,但不知何故设法使用 ACM 包运行它们。现在我正在尝试做作业并运行我自己的代码。我在该项目中创建了一个“项目”和一个.java文件。我不知道如何运行它。我不断收到以下信息:

Error: Could not find or load main class Pyramid.

我认为这是因为程序没有访问 ACM 包。下面是代码,尽管我认为我编写的任何代码都会发生这种情况。任何帮助,将不胜感激。

非常感谢。

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class GRectExample extends GraphicsProgram {

  public void run() {
    GRect rect = new GRect(100, 50, 125, 60);
    rect.setFilled(true);
    rect.setColor(Color.RED);
    add(rect);
  }

}
4

3 回答 3

1

看起来你必须告诉 Eclipse 在哪里找到 ACM 包,大多数时候它不能假设确切的位置。

右键单击您的项目文件夹并选择Properties

选择Java Build Path选项并单击“ Add External JARs ”,这会将其包含到您的项目中...

在此处输入图像描述

于 2013-12-19T17:41:44.020 回答
1

在 GRectExample 类中创建一个 main 方法,例如

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class GRectExample extends GraphicsProgram {

  public void run() {
    GRect rect = new GRect(100, 50, 125, 60);
    rect.setFilled(true);
    rect.setColor(Color.RED);
    add(rect);
  }

  public static void main(String args[])
  {
    new GRectExample().run();
  }
}
于 2013-12-19T17:29:36.633 回答
0

Not too familiar with Eclipse, but here's a suggestion:

  1. Right - Click on the Project folder
  2. click Properties at the bottom
  3. click Run/Debug Settings
  4. Make sure your Launching class is the list. Click on it, make sure it's the Main class
  5. Make sure you use the fully qualified name i.e. mypackage.MyClass
  6. Also try clicking all of them in the list. And make sure only the one you want to be the launching class has the Main Class field filled in.
于 2013-12-19T17:51:26.130 回答