EXECUTING A PROGRAM WITH SEVERAL CLASSES

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
Java 
 724  View(s)
Ratings:
Rate this:
I am working my way through "Head First Java" and typing the code in the book into Notepad++ as I go. In the first few chapters the code was simple and only had one class (main). Now the code has two or more classes. Originally I would compile the code in the Command Window by typing "javac" and the program's name. After it compiled I would execute the program by typing "java -classpath . " and the program's name. However, now when program has several classes I get the following error: Could not find or load main class. Below is a program I am having issues with... does it need to be saved as two separate files? 
class DogTestDrive {
     
    public static void main (String [] args) {
        Dog one = new Dog();
        one.size = 70;
        Dog two = new Dog();
        two.size = 8;
        Dog three = new Dog();
        three.size = 35;
         
        one.bark();
        two.bark();
        three.bark();
    }
}
class Dog{
 
    int size;
    String name;
     
    void bark() {
        if (size > 60) {
            System.out.println("Woof! Woof!");
            } else if (size > 14) {
            System.out.println("Ruff! Ruff!");
            } else {
            System.out.println("Yip! Yip!");
            }
        }
    }
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Executing a program with several classes

    Why are you asking if you know the answer already? That is indeed the case.
    They should be in Dog.java and DogTestDrive.java

    Then compile and run the class with the main method (DogTestDrive) as you did before.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!