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

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(
      "craigsdennis",
      "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

Hi,

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.

Thanks!

James

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