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 (Retired) Meet Objects Creating Classes

burton helmick
burton helmick
2,853 Points

Help with Error: Main method not found in class PezDispenser, please define...

I can't find anywhere to see the teachers code so I have completely started over trying to fix problems. Now after the first video I am getting this error.

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(); System.out.printf("The dispenser character is %s/n", dispenser.mCharacterName); } }

PezDispenser.java public class PezDispenser { public String mCharacterName = "yoda"; }

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

Any help would be great.

1 Answer

James Vaughan
PLUS
James Vaughan
Courses Plus Student 4,462 Points

Hi Burton,

I've just compiled your code and it works okay.

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();
        System.out.printf("The dispenser character is %s/n", dispenser.mCharacterName);
    }
}

PezDispenser.java

public class PezDispenser {
    public String mCharacterName = "yoda";
}

I think you may be executing it incorrectly though. The main method is in Example.java as that is the name of the console application you are writing. When you compile it, it in turn compiles PezDispenser.java.

You should be running it as follows: (within the workspace)

clear && java Example.java && java Example

For your code above this returns the following for me:

we are making a new Pez Dispenser. The dispenser character is yoda/n

Note you've got the incorrect slash in your System.out.printf line, should read:

System.out.printf("The dispenser character is %s\n", dispenser.mCharacterName);

Hope that helps

burton helmick
burton helmick
2,853 Points

Thank you so much! You have saved me hours of frustration...

I am struggling and decided to start the course over, but sadly there isn't a restart button so when I erased my previous work I started in a file with the wrong name.

clear && javac PezDispenser.java && java PezDispenser wont run a file named Example.java

James Vaughan
James Vaughan
Courses Plus Student 4,462 Points

Burton,

Not sure if your comment was a question or not. But you shouldn't be trying to run the PezDispenser class directly. The entry point into the application is within Example.java that's this bit:

public static void main(String[] args) {

When you run Example it in turns calls the compiled PezDispenser class when you create a new object.