Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,694 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"?