user5491858
user5491858

Reputation: 23

Why am I getting an error for main method not being found when running an example program?

I'm trying to run a program, (which by the way, is not mine, and am trying to understand it as a way of learning basic Java as a beginner), but I'm getting an error from Eclipse stating:

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

Yet, I see public static void main(String[] args)in there? I first created a Java Project with the project name 'Door', selected 'Use an execution environment JRE: JavaSE-1.8', and selected 'Use project folder as root for sources and class files'. Then, I created a Java Class with the Source folder and Name being 'Door' and I selected public as the modifier and had no method studs selected. Am I selecting the wrong options?

The code:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.util.Scanner;

public class TestDoor
{
static Door Door1;
static Door Door2;
static Door Door3;

static Scanner input=new Scanner(System.in);

public static void main(String[] args)
{
    Door1=new Door("Enter");
    Door2=new Door("Exit");
    Door3=new Door("Treasure");

    Door1.setOpen();
    Door1.setUnlocked();
    Door3.setOpen();

    boolean done = false;
    int choice;

    do
    {
        chooseDoor();
        choice = input.nextInt();

        switch(choice)
        {
            case 1:
                DOOR1();
                break;
            case 2:
                DOOR2();
                break;
            case 3:
                DOOR3();
                break;
            case 4:
                System.out.println("\n\n---------- Door Status ----------\n");

                if(Door1.isLocked()==true)
                {
                    System.out.print("ENTER door is LOCKED and ");
                }

                else
                {
                    System.out.print("ENTER door is UNLOCKED and ");
                }

                if(Door1.isClosed()==true)
                {   
                    System.out.print("CLOSED\n\n");
                }

                else
                {
                    System.out.print("OPEN\n\n");
                }

                if(Door2.isLocked()==true)
                {
                    System.out.print("EXIT door is LOCKED and ");
                }

                else
                {
                    System.out.print("EXIT door is UNLOCKED and ");
                }

                if(Door2.isClosed()==true)
                {   
                    System.out.print("CLOSED\n\n");
                }

                else
                {
                    System.out.print("OPEN\n\n");
                }

                if(Door3.isLocked()==true)
                {
                    System.out.print("TREASURE door is LOCKED and ");
                }

                else
                {
                    System.out.print("TREASURE door is UNLOCKED and ");
                }

                if(Door3.isClosed()==true)
                {   
                    System.out.print("CLOSED\n\n");
                }

                else
                {
                    System.out.printf("OPEN\n\n");
                }

                System.out.println("---------- Door Status ----------\n");
                break;
            default:
                System.out.println("<<<< Out of choice range select again >>>>\n");
            }

    }while(!done);
}

public static void Choices()
{
    System.out.println("\n--------- choices ---------\n\n");
    System.out.println("1. OPEN door");
    System.out.println("2. CLOSE door");
    System.out.println("3. LOCK door");
    System.out.println("4. UNLOCK door");
    System.out.println("5. Check current door status");
    System.out.println("6. Go Back to choose another door");
    System.out.print("\n\nEnter Choice: ");
}

public static void chooseDoor()
{
    System.out.println("\n\n--------- Choose Your Door --------\n\n");
    System.out.println("1. Enter door");
    System.out.println("2. Exit door");
    System.out.println("3. Treasure door");
    System.out.println("4. All Door Statuses");
    System.out.print("\n\nEnter Choice: ");
}

public static void DOOR1()
{
    int choice;
    boolean done = false;
    do
    {
        Choices();
        choice=input.nextInt();

        /*for(int i=0;i<6;i++)
        {
            System.out.println();
        }*/

        switch(choice)
        {
            case 1:
                Door1.open();
                break;
            case 2:
                Door1.close();
                break;
            case 3:
                Door1.lock();
                break;
            case 4:
                Door1.unlock();
                break;
            case 5:
                if(Door1.isLocked()==true)
                {
                    System.out.println("---------- The ENTER Door Status ----------\n");
                    System.out.print("Door is LOCKED and ");
                }
                else
                {
                    System.out.println("---------- The ENTER Door Status ----------\n");
                    System.out.print("Door is UNLOCKED and ");
                }
                if(Door1.isClosed()==true)
                {   
                    System.out.print("CLOSED\n\n");
                    System.out.println("---------- The ENTER Door Status ----------\n");
                }
                else
                {
                    System.out.printf("OPEN\n\n");
                    System.out.println("---------- The ENTER Door Status ----------\n");
                }
                break;
            case 6:
                done = true;
                break;
            default:
                System.out.print("<<<< Out of choice range select again >>>>\n");
            }
    }while(!done);
}

public static void DOOR2()
{
    int choice;
    boolean done = false;

    do
    {
        Choices();
        choice=input.nextInt();

            /*for(int i=0;i<6;i++)
            {
                System.out.println();
            }*/
        switch(choice)
        {   
            case 1:
                Door2.open();
                break;
            case 2:
                Door2.close();
                break;
            case 3:
                Door2.lock();
                break;
            case 4:
                Door2.unlock();
                break;
            case 5:
                if(Door2.isLocked()==true)
                {
                    System.out.println("---------- The EXIT Door Status ----------\n");
                    System.out.print("Door is LOCKED and ");
                }
                else
                {
                    System.out.println("---------- The EXIT Door Status ----------\n");
                    System.out.print("Door is UNLOCKED and ");
                }
                if(Door2.isClosed()==true)
                {   
                    System.out.print("CLOSED\n\n");
                    System.out.println("---------- The EXIT Door Status ----------\n");
                }
                else
                {
                    System.out.printf("OPEN\n\n");
                    System.out.println("---------- The EXIT Door Status ----------\n");
                }
                break;
            case 6:
                done = true;
                break;
            default:
                System.out.print("<<<< Out of choice range select again >>>>\n");
        }
    }while(!done);
}

public static void DOOR3()
{
    int choice;
    boolean done = false;

            do
            {
                Choices();
                choice=input.nextInt();

                /*for(int i=0;i<6;i++)
                {
                    System.out.println();
                }*/

                switch(choice)
                {
                    case 1:
                        Door3.open();
                        break;
                    case 2:
                        Door3.close();
                        break;
                    case 3:
                        Door3.lock();
                        break;
                    case 4:
                        Door3.unlock();
                        break;
                    case 5:
                        if(Door3.isLocked()==true)
                        {
                            System.out.println("---------- The TREASURE Door Status ----------\n");
                            System.out.print("Door is LOCKED and ");
                        }
                        else
                        {
                            System.out.println("---------- The TREASURE Door Status ----------\n");
                            System.out.print("Door is UNLOCKED and ");
                        }
                        if(Door3.isClosed()==true)
                        {   
                            System.out.print("CLOSED\n\n");
                            System.out.println("---------- The TREASURE Door Status ----------\n");
                        }
                        else
                        {
                            System.out.printf("OPEN\n\n");
                            System.out.println("---------- The TREASURE Door Status ----------\n");
                        }
                        break;
                    case 6:
                        done = true;
                        break;
                    default:
                        System.out.print("<<<< Out of choice range select again >>>>\n");
                }
            }while(!done);
}
}

Upvotes: 1

Views: 167

Answers (3)

Aurasphere
Aurasphere

Reputation: 4011

Sometimes this error may come from build path problems. If you are using JavaFX in your program or any other library, make sure you properly added the jars to the build path. To do so, right click on the project -> Properties -> Build Path -> Add External Jar.

Also, if you want to always know what kind of problem you have on your project, I'd suggest to take a look at the Problems view. If it's not enabled in your Eclipse, you can just make visible by going into Window -> View.

Upvotes: 1

nhouser9
nhouser9

Reputation: 6780

The class Door should have a main method. You have not posted that class, so we cannot look at it, but your problem should be solved if you define a main method within Door.

Upvotes: 1

Som Bhattacharyya
Som Bhattacharyya

Reputation: 4112

Well looks like you have copied an incomplete example. The class you posted uses objects of the Door class which is not there ! Look for other snippets at the source you copied from. There is no Door class here.

Upvotes: 1

Related Questions