Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Harnessing the Power of Objects Constants

Static Keyword: Main Method not found in PezDispenser, please define the main method?

I followed the entire Static keyword video and created the static variable inside my PezDispenser.java file, but when I used it on my Example.java file I get the above error. In the video, Craig didn't run the Example class from the console or used it on jshell; however, I compiled and ran the Example.java class and I got that error.

PezDispenser.java:

class PezDispenser {
  public static final int MAX_PEZ = 12;
  final private String characterName;


  public PezDispenser(String characterName) {
    this.characterName = characterName;
  }
  public String getCharacterName() {
    return characterName;
  }


}

Example.java:

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new PEZ Dispense");
    System.out.printf("FUN FACT: There are %d number of yodas in our pez dispenser %n", PezDispenser.MAX_PEZ);
    PezDispenser dispenser = new PezDispenser("Yoda");
    System.out.printf("The dispenser is  %s %n",
                      dispenser.getCharacterName());
  }   

}

Compile Error:

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

What's going on?

1 Answer

Simon Coates
Simon Coates
28,693 Points

you need to run the class that contains the main method. Your error message indicates that it seems to be looking for it in the PezDispenser file, not the Example file. are you running it with "Java Example"?