Brendanp
Brendanp

Reputation: 17

How do i get rid of this message Error: Main method not found in class

I have been working on this Java game lately. I was trying to add music with an audio clip but it did not work so I took out the code that was in my main and deleted the main because I did not need it any more. All the code was the same from before I implemented the music but now this message is popping up and not letting me run my game:

Error: Main method not found in class com.illuminationsco.GoNerdGo.Entities.Obstacles, please define the main method as: public static void main(String[] args)

When I try putting back the main it does not run at all!

If anybody could help me that would be great!

Here is my code:

Bully bully = new Bully();
Nerd nerd = new Nerd();

static BufferedImage[] sprites;

public static float x, y, velX = 5.5f, velY = 5.5f;
public static int whatObstacle = 5;

public static int basketBall = 6;
public static int lava = 2;
public static int trash = 1;

public static boolean trashOpen = false;

public Rectangle getBounds() {
    return null;
}

public Obstacles() {
    int width = 100;
    int height = 100;
    int columns = 2;
    int rows = 2;

    BufferedImage spriteSheet = null;
    try {
        spriteSheet = ImageIO.read(new File(
                "res/Images/ObstacleSpriteSheet.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    sprites = new BufferedImage[rows * columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width,
                    j * height, width, height);
        }
    }
}

public void tick() {
    if (whatObstacle >= basketBall && Game.gos == null) {
        x += velX;
        y += velY;
    }

    if (x <= 0 || x >= 800 - 35) {
        velX *= -1;
    }
    if (y <= 425 || y >= 600 - 35) {
        velY *= -1;
    }
}

public void render(Graphics g) {
    if (whatObstacle >= basketBall) {

        // Displaying basketball

        g.drawImage(sprites[3], (int) x, (int) y, null);

        Rectangle basketballRect = new Rectangle((int) x, (int) y, 35, 35);

        if (nerd.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying lava pool
    } else if (whatObstacle >= lava) {

        g.drawImage(sprites[2], (int) x, (int) y, null);

        Rectangle lavaRect = new Rectangle((int) x + 5, (int) y + 7, 80, 19);

        if (nerd.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying trash can

    } else if (whatObstacle == trash) {

        if (trashOpen) {
            g.drawImage(sprites[1], (int) x, (int) y, null);
        } else {
            g.drawImage(sprites[0], (int) x, (int) y, null);
        }

        Rectangle trashRect = new Rectangle((int) x, (int) y, 60, 97);

        if (bully.getBounds().intersects(trashRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
            Obstacles.trashOpen = true;
        }
    }
}

Upvotes: 0

Views: 1297

Answers (3)

Prakhar
Prakhar

Reputation: 536

Dude, if you are extending an Applet Class then add the following code within comments after declaring your class:

/*
<applet code = "class-name" width = 300 height = 200></applet>
*/

then run it using

appletviewer file-name.java

If it is normally extending Frame then :

add a method like this:

public static void main(String argsp[])
{
new Constructor();
}

It will work.

Upvotes: 0

Patricia
Patricia

Reputation: 2865

Insert a main-method in your class - a public static void main(String[] args)! When you run a Java-program, then the output is only that that is in this method. So you need it always!

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201537

From Setting an Application's Entry Point,

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname

The value classname is the name of the class that is your application's entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

Upvotes: 0

Related Questions