Home > DeveloperSection > Forums > Executing a program with several classes
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    April-29-2015 12:24 AM

 Java Java 
Ratings:


 1 Reply(s)
 458  View(s)
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!");
            }
        }
    }


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    April-29-2015 7:15 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick