Stanford CS106a newbie - Having trouble compiling in Eclipse

1.3k views Asked by At

I'm following along with Stanford's CS106a class and trying to do the assigments. I had difficulty running the sample code from the book but somehow managed to run them with the ACM package. Right now I'm trying to do the assignments and run my own code. I've created a "project" and a .java file in that project. I don't know how to run it though. I keep getting the following:

Error: Could not find or load main class Pyramid.

I think it is because the program isn't accessing the ACM package. Below is the code although I think it would happen with any code I write. Any help would be appreciated.

Thanks so much.

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);
  }

}
3

There are 3 answers

3
Kiet Thanh Vo On

Create a main method inside GRectExample class, for examle

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();
  }
}
0
Salvador Valencia On

Looks like you have to tell Eclipse where to locate the ACM package, most of the times it can't assume the exact location.

Right click on your project folder and select Properties.

Select the Java Build Path option and click on the "Add External JARs" and that will include it into your project...

enter image description here

0
Paul Samsotha On

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.