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 Meet Objects Constructors

Ranak Bansal
Ranak Bansal
1,207 Points

Main method not found in class error

Hello,

Whenever I run my code, I keep getting the following 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

Here is my code:

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 Dispenser"); PezDispenser dispenser = new PezDispenser("Donatello"); System.out.printf("The dispenser is %s %n", dispenser.getCharacterName() ); }

}

PezDispenser.java

class PezDispenser { private String mcharacterName;

public PezDispenser(String characterName) { mcharacterName = characterName; }

public String getCharacterName() { return mcharacterName; } }

2 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

First of all I need to ensure one thing:

Do you use: javac PezDispenser.java command to compile your code and java PezDispenser to run your compiled code?

Because it sounds like so. If this is true it will invoke error as shown in your case.

In order to compile and run your code you need to use:

javac Example.java && java Example

Because the main method is resided inside the Example.java class not in the PezDispenser.java class.

I hope this can help. Happy coding

Ranak Bansal
Ranak Bansal
1,207 Points

Yes, this was the error, thank you