Error in Eclipse - Mainclass not found

2019-02-28 09:33发布

问题:

I'm new in programing and I like it pretty much. I've just downloaded Eclipse and I got an error I can't help me with. Unfortunately it's in German but the meaning is something like: "Main class not found" - "Fehler: Hauptklasse konnte nicht gefunden oder geladen werden"

I understand that it has something to do with "public static void main(String [] args)". Due to the fact that this is totally new to me it would be cool you could assist me.

Below the error source code;

/**
 * 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;
        }
    }
}

回答1:

when you run java it runs main method that i don't see in your class so basically eclipse is telling you: "what do you want me to run?", you should implement it:

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


回答2:

I found another error.

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

Method can't be called 'int'. This name is reserved by the Java language.



回答3:

When developing a core-java application, all you need to do is to have a main method (ofcourse with the functionality :P) in it which is the first code fragment JVM Searches for when you try to run your application. For the above code, try this:

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

Just make sure to have a main method for executing your java code. Happy Coding :)



回答4:

the access point for java is the main method.. every program must access from a main method. and in main method, you need to create an instance of your class to use the method inside main method like following:

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

and also public int (int carry) // Setting carry into the elevator is not valid format in Java, you have to define a method name like

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


回答5:

Thanks for the great answers and the error correction. What I still don't understand is where to put public static void main (Strng[] args) and why at the position X. My understanding is as followed.

  1. Definition of instance variables
  2. Set constructor
  3. Definition of methodes

Do I have to put "public static void main (Strng[] args)" before 1 or after 3?

And also When I look at "public static void main (Strng[] args)" from my point of view I create an String Array, correct? But why is that the case? All I want to do is to work with numbers.

Cheers & thank you, Sunny



回答6:

I've just coded the following and it worked pretty good.

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

public class Person {
    // Variables
    public int weight;

    // Constructors
    public Person() {
        weight = 80;
    }

    // Methods
    public void setPersWeight(int weight) // setting a persons weight
    {
        this.weight = weight;

        if (weight <= 0) {
            switch (weight) {
            case 0:
                System.out
                        .println("Person must have a weight between 1-160 KG");
                break;

            default:
                System.out.println("A person can't weigh less than 0KG");
                break;
            }
        } else {
            if (weight > 160) {
                System.out.println("To heavy - The elevator will not work");
            }
            else
            {
                System.out.println("The person in the Elevator weights " +weight +"KG.");
            }
        }
    }

    public static void main(String[] args) {
        Person Hans = new Person();
        Hans.setPersWeight(-1);
    }
}

Thanks to all of you guys - pretty cool to get such results.

but still - do you maybe have an answer to the question I asked above:

"And also When I look at "public static void main (Strng[] args)" from my point of view I create an String Array, correct? But why is that the case? All I want to do is to work with numbers."

cheers Sunny



回答7:

We can't run a Java program without

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

The program only executs the main method. In the main method you can create objects like

Elevator elevator = new Elevator();

You can put the main method anywhere.