简体   繁体   中英

Why can I not load/find the main class?

Basically I get the error message: "Error: Could not find or load main class Driver" Why Am I getting this error? This is my first instance working with classes, so I am unsure of my syntax in my code. Any help is appreciated, thanks.

public class Person {
private String name;//a variable that contains a person's name
private int age;//a variable that contain's a person's age

public class Driver{
    public void main(String[] args )
    {
        String name1="John";
        int age1=30;
        Person person1= new Person();
        person1.setName(name1);
        person1.setAge(age1);
        System.out.print(person1.getName());
        System.out.print(person1.getAge());
    }
}
//returns the age of a person
public int getAge(){
    return age;
}

//returns the name of a person
public String getName()
{
    return name;
}

//changes name of a person
public void setName(String s)
{
    name=s;
}

//changes age of a person
public void setAge(int x)
{
    age=x;
}

}

The JVM can't locate the main method as specified by the JLS

The method main must be declared public, static , and void.

You need to make the inner class a top level class and make the main method static (since static methods can only belong to top level classes)

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

public void main(String[] args )应该是public static void main(String[] args )

You need to make inner class static as well as main method. You can't put static method on instance inner class.

public class Person {
 public static class Driver{
    public static void main(String[] args) {
        ....
    }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM