【问题标题】:Error in Eclipse - Mainclass not foundEclipse 中的错误 - 找不到主类
【发布时间】:2014-02-25 03:09:32
【问题描述】:

我是编程新手,我非常喜欢它。我刚刚下载了 Eclipse,但遇到了一个无法帮助我解决的错误。不幸的是,它是德语,但含义类似于:“未找到主要课程”-“Fehler: Hauptklasse konnte nicht gefunden oder geladen werden”

我知道它与“public static void main(String [] args)”有关。 由于这对我来说是全新的,所以你能帮助我会很酷。

下面的错误源代码;

/**
 * Write a description of class Light here.
 * 
 * @author (Sunny)
 * @version (31.01.2014)
 */

public class Elevator {
    // Variables

    int maxCarr; // max. carry in KG
    int currentCarr; // current state of carry measured in people
    boolean fillCondition; // filed or empty - value false = empty
    int currentStage; // stage where elevator remains


    // Constructor
    public Elevator() // initiation
    {
        maxCarr = 1600;
        currentCarr = 0;
        fillCondition = false;
        currentStage = 0;
        System.out.println("**********");
        System.out.println("*        *");
        System.out.println("*        *");
        System.out.println("*        *");
        System.out.println("*        *");
        System.out.println("*        *");
        System.out.println("*        *");
        System.out.println("**********");
    }

    public int  (int carry) // Setting carry into the elevator
    {
        currentCarr = carry;
        if (currentCarr != 0) {
            fillCondition = true;
            return 1;
        } else {
            return 0;
        }
    }

    public void move(int stage) {
        if (stage > currentStage) {
            System.out.println("moving up");
            currentStage = stage;
        } else {
            System.out.println("moving down");
            currentStage = stage;
        }
    }
}

【问题讨论】:

  • 只需将public static void main(String[] args){Elevator();} 添加到您的代码中

标签: java eclipse main


【解决方案1】:

当你运行 java 时,它运行 main 方法,我在你的课堂上看不到,所以基本上 eclipse 告诉你:“你想让我运行什么?”,你应该实现它:

public static void main(String[] args){
    // code here
}

【讨论】:

    【解决方案2】:

    我发现另一个错误。

      public int  (int carry) // Setting carry into the elevator
    {
        currentCarr = carry;
        if (currentCarr != 0) {
            fillCondition = true;
            return 1;
        } else {
            return 0;
        }
    }
    

    方法不能被调用'int'。此名称由 Java 语言保留。

    【讨论】:

      【解决方案3】:

      在开发 core-java 应用程序时,您需要做的就是在其中有一个 ma​​in 方法(当然还有功能:P),这是 JVM 在您搜索时搜索的第一个代码片段尝试运行您的应用程序。对于上面的代码,试试这个:

      public static void main (String [] args) {
      
      //Now, make an instance of your class to instantiate it.
      
      Elevator obj = new Elevator();
      
      //Then,as per the desired functionality. Call the methods in your class using the reference.
      
      //obj.move(val of stage);
      
      obj.move(10);
      }
      

      只要确保有一个 main 方法来执行你的 java 代码。快乐编码:)

      【讨论】:

        【解决方案4】:

        java 的访问点是 main 方法。每个程序都必须从 main 方法访问。 在 main 方法中,您需要创建类的实例以使用 main 方法中的方法,如下所示:

        public static void main(String [] args){
          Elevator elevator = new Elevator();
          elevator.move(1);
          ...
        }
        

        public int (int carry) // Setting carry into the elevator 在 Java 中也不是有效的格式,您必须定义一个类似的方法名称

        public int setCarry(int carry) // Setting carry{
          //your code
        }
        

        【讨论】:

          【解决方案5】:

          没有

          ,我们无法运行 Java 程序
          public static void main(String[] args) {
          }
          

          程序只执行main方法。在 main 方法中,您可以创建对象,如

          Elevator elevator = new Elevator();
          

          你可以把main方法放在任何地方。

          【讨论】:

            猜你喜欢
            • 2014-03-28
            • 2014-11-18
            • 2020-05-15
            • 2015-07-15
            • 2012-04-18
            • 1970-01-01
            • 2013-06-20
            • 1970-01-01
            相关资源
            最近更新 更多