Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Eclipse - "Could not find or load main class conditionalDemo"

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Eclipse - "Could not find or load main class conditionalDemo"

    OK I've been playing around, following some tutorials... I had to classes in a project that seemed to conflict, I'm not sure why. Basically when I ran one it gave me the result of the other...

    So I created a new Java project, deleted one of the classes and pasted it into the new project... However it still didn't work, and basically now when I try to run the program I get this error :



    I've not encountered this before, I'm sure it's fairly simple but I don't really know how to solve it.

    I'm seen some post's saying to restate the files path, clean bin and stuff.... I've not encountered this before though so I wouldn't be too sure what to do if I did know which one to try!

    cheers

    --- Update ---

    I think I've still got the file on my computer :



    I'm not sure though.... Should this be deleted?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    There is more than one odd about the graphic you posted, but the main odd thing is that the names don't match. The black line says it was running ConditionalDemo1, but the error message says it was looking for ConditionalDemo. Is the class name the same as the file name without the '.java'? Apparently not. Fix that and report back.

    Responding to your update, it shouldn't matter.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    Hi Greg - here's a screen shot, I've changed the class name to 'ConditionalDemo' (is this what you meant?)



    --- Update ---

    I won't actually let me run any programs now, just returns that error each time

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    The project's name is ConditionalDemo2. That's fine.

    The filename is ConditionalDemo.java, and the class' name is ConditionalDemo. so those are as they should be.

    The black writing in the Console view says Eclipse was trying to run ConditionalDemo1. That doesn't match the code or project structure.

    Click on the package name, ConditionalDemo2, in the Project Explorer pane in the left pane. Select F5 (Refresh). Then right click on the class name, ConditionalDemo, in the Project Explorer, select "Run as" then "Java Application".

    That should fix your setup and run the program. If not, post any new errors or unexpected behaviors.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Scren (January 19th, 2014)

  6. #5
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    Ah bingo, that's weird... It returns this error for every program (in this workspace) so each time I open a program I just do this?

    I've just tested it and it works... Closed Eclipse and re-opened it and the projects that I had done that to stayed 'worked'...

    Is there some kind of 'cache' that I should clear?



    Cheers though greg!

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    Not that I know of. Eclipse keeps the details of the workspace in an xml file or similar in each workspace, and when that file becomes corrupted either on purpose or by accident, there's no way to edit it or fix it that I'm aware of. Opening and closing is probably a good start, but if it becomes hopelessly muddled, one might have to move the code to a new workspace. The code should survive.

    Next time, either copy and past the source code into a class of the same name in the new workspace/project OR use the import feature to import existing projects into workspace. There are several ways to do it right, probably just as many ways to do it wrong, and you'll figure them all out if you keep at it long enough. You should also figure out how to recover from the wrong ways, though sometimes it's easiest just to start over and use one of the right ways.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    Scren (January 19th, 2014)

  9. #7
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    I think I confused it by dragging a file from one package to another... Will be reading about the import / export feature next time! cheers

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse - "Could not find or load main class conditionalDemo"

    That will normally work as long as you do it using the Package Explorer, and you allow Eclipse to "update references." I think that's what it's called. Eclipse gives you the option to skip that step, and if you do, you're likely to end up with a confused workspace that you'll have to clean up manually. That's what you just did, and it wasn't that big of a deal.

    Also, the "Run" button and corresponding run list will become confused at times. When selected, the run button attempts to run the currently selected class, but if the currently selected class doesn't contain a main() method, it will run the last class run at the top of the run list. If the workspace becomes muddled, the run list may not be correct anymore, containing a class that no longer exists or whose name has changed, etc. Running the desired class may require right clicking the class and selecting Run As as you did earlier.

    The good news is that you have to work incredibly hard to confuse Eclipse so badly that source files are lost.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    Scren (January 19th, 2014)

Similar Threads

  1. Replies: 4
    Last Post: September 3rd, 2012, 02:47 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM