Search code examples
javaprogram-entry-point

JAVA - cannot find main


I am trying out an association example in java; when compiling, it gives the error message saying "java cannot find main in CarClass". I double checked the "main" syntax, tried multiple versions - still doesn't work. Appreciate any help!

class CarClass{
   String carName;
   int carId;
   CarClass(String name, int id)
   {
    this.carName = name;
    this.carId = id;
   }
}
class Driver extends CarClass{
   String driverName;
   Driver(String name, String cname, int cid){
    super(cname, cid);
    this.driverName=name;
   }
}
class TransportCompany{
   public static void main(String args[])
   {
    Driver obj = new Driver("Andy", "Ford", 9988);
    System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
   }
}

Thanks.


Solution

  • If all your classes (CarClass, Driver & TransportCompany) are all in the same .java file, the class that contains the main method and the file name should be the same.

    In your case, add the keyword public to TransportCompany - that way the JVM knows to look for the main method in TransportCompany