1

I have trawled through the internet for a solution to this and I still don't understand why I am getting the following error message:

"Error: Main method not found in class DrumKit, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

Here is my code:

DrumKit.java

class DrumKit {
    boolean topHat = true;
    boolean snare = true;

    void playTopHat() {
        System.out.println("ding ding da-ding");
    }

    void playSnare() {
        System.out.println("bang bang ba-bang");
    }
}

DrumKitTestDrive.java

class DrumKitTestDrive {
    public static void main(String [] args) {

        DrumKit d = new DrumKit();
        d.playSnare();
        d.snare = false;
        d.playTopHat();
        if (d.snare == true) {
            d.playSnare();
        }
    }
}

I am using InteliJ and I input the command javac DrumKitTestDrive.java followed by java DrumKit.

This is from an exercise from the Head First Java series and my answer is exactly the same as the one from the book. But I still encounter this error.

Jens
  • 67,715
  • 15
  • 98
  • 113
LearnerCode
  • 159
  • 4
  • 17

2 Answers2

2

Your class containing the main method must be public

public class DrumKitTestDrive {
    public static void main(String [] args) {

        DrumKit d = new DrumKit();
        d.playSnare();
        d.snare = false;
        d.playTopHat();
        if (d.snare == true) {
            d.playSnare();
        }
    }
}

For more informations about access modifier see https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Also you try to run the class DrumKit which does not have a main mathod. You have to run java DrumKitTestDrive

Jens
  • 67,715
  • 15
  • 98
  • 113
  • @KOF can you explain why you have unaccepted my answer? – Jens Dec 16 '19 at 15:27
  • I can only guess, but your first sentence is wrong. – bastet Dec 17 '19 at 08:53
  • @bastet Why do you think so? – Jens Dec 17 '19 at 08:56
  • Funny question. Well, open the JLS, lookup "public static void main(" and count how many classes are "public". I would certainly prefer to point you to the correct chapter and verse, but I didn't find it clearly stated (nor the contrary of course): the only stated constraint is that `main` must be public. However, dozens of examples in the specification seem good enough to me. I could also ask: why makes you think you are right? – bastet Dec 17 '19 at 09:06
  • @bastet: *I could also ask: why makes you think you are right?* sorry did I asked you. I never used main classes without public modifier. – Jens Dec 17 '19 at 09:11
1

The main method is in DrumKitTestDrive.java, not in DrumKit.java. Therefore you must type java DrumKitTestDrive to run the program.

Note that contrary to what the other answer claims, it's absolutely not mandatory that the class having main be public. The main method must be public however. See chapter 12 of Java Language Specification. Incidentally, in most examples of the JLS, classes have no modifier. This was already addressed by this answer.

bastet
  • 93
  • 4