Reputation: 179
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.
Upvotes: 1
Views: 196
Reputation: 93
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.
Upvotes: 1
Reputation: 69440
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
Upvotes: 2