-2

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

}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
redgabe
  • 61
  • 7

3 Answers3

1

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();
  }
}
Kiet Thanh Vo
  • 399
  • 5
  • 16
  • thanks for the reply. So I did what you suggested with the following code. I realized I put the wrong code in the question but it should run the same. I clicked on Run, it asked me which type to Run and I chose the type with the same name as the file and I still got "Error: Could not find or load main class Pyramid". – redgabe Dec 19 '13 at 17:38
  • here is the code: import acm.graphics.*; import acm.program.*; import java.awt.*; public class Pyramid extends GraphicsProgram { public static void main() { GRect rect = new GRect (100,100, BRICK_WIDTH, BRICK_HEIGHT) add(rect) } private static final int BRICK_WIDTH = 30 // pixels private static final int BRICK_HEIGHT = 12 //pixels private static final int BRICKS_IN_BASE = 14 //bricks on the bottom of pyramid public static void main(String args[]){ new Pyramid().run(); } } – redgabe Dec 19 '13 at 17:40
  • Unfortunately you are not showing all of the code. Either way you will have to pull it apart (divide and conquer). Start commenting out lines of code and compile again. Is that add() inherited? – Salvador Valencia Dec 19 '13 at 18:20
1

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

Salvador Valencia
  • 1,330
  • 1
  • 17
  • 26
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.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720