为什么运行示例程序时找不到主要方法的错误?

时间:2016-04-21 06:38:36

标签: java eclipse

我试图运行一个程序,(顺便说一句,这不是我的,并且我试图把它理解为一种学习基础Java作为初学者的方式),但是我得到了一个Eclipse中的错误说明:

  

错误:在类门中找不到主要方法,请将主方法定义为:      public static void main(String [] args)   或者JavaFX应用程序类必须扩展javafx.application.Application

然而,我在那里看到public static void main(String[] args)? 我首先使用项目名称' Door'创建了一个Java项目,选择'使用执行环境JRE:JavaSE-1.8',并选择'使用项目文件夹作为root用户来源和类文件'。然后,我创建了一个Java类,其中包含Source文件夹,Name是' Door'我选择public作为修饰符,没有选择方法。我选择了错误的选项吗?

代码:

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

3 个答案:

答案 0 :(得分:1)

好像你复制了一个不完整的例子。 您发布的类使用Door类的对象,但不存在! 在您复制的源代码中查找其他代码段。这里没有Door课程。

答案 1 :(得分:1)

班级Door应该有一个主要方法。您尚未发布该类,因此我们无法查看它,但如果您在Door中定义主方法,则应解决您的问题。

答案 2 :(得分:1)

有时此错误可能来自构建路径问题。如果您在程序或任何其他库中使用JavaFX,请确保将jar正确添加到构建路径中。为此,请右键单击项目 - &gt;属性 - &gt;构建路径 - &gt;添加外部Jar。

另外,如果你想知道你的项目有什么样的问题,我建议你看看问题视图。如果它没有在你的Eclipse中启用,你只需进入Window - &gt;就可以看到它。图。