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.


Error: Could not find or load main class Example

Hello -

I have no idea why this isn't working, this is for the Object Inheritance Java course. When I try to compile and run it gives the error "Could not find or load main class Example". I have the Example.java file in the same parent directory as the com folder. This format worked in the previous exercise.

package com.teamtreehouse;

import java.util.Date;

public class Example {

  public static void main(String[] args) {

    Treet treet = new Treet(
      "this is a tweet",
      new Date(1489438425L)

    System.out.printf("This is a new Treet: %s %n", treet);

Matthias Margot
Matthias Margot
7,236 Points


A couple of reasons why that might be:

-the most obvious one first: you might not have compiled your file ('javac Example.java' in the console)

-when you created the file you named it differently than Example.java (in the file directory not in actual code), in which case you need to right click on your file class associated with Example.java and choose rename and rename it to the same name as your class.

-this can't be the case if the code you're showing is your actual code but this error also occurs if your main() method isn't in the file you are trying to execute from

Make sure all of these properties are matched and try running it again.

Hope this will make your code run

Matthias :)

2 Answers

Hi Matthias,

That is my actual code posted above. I ran the compiler and had both the class and the file named Example.java, but it still wasn't working. I ended up just starting with a fresh workspace and copying in the code from earlier exercise files, so I'm good to go.



Matthias Margot
Matthias Margot
7,236 Points

Happy to hear that, you know in the end it might just have been a bug in the current treehouse workspace if indeed you did do exactly the same code + file names in the new one.

moritz lehnhardt
moritz lehnhardt
429 Points

I have the same problem today! tried everything of the above. Support is in the loop