Pergunta

I started a small Java project on Eclipse with two classes that are in the (default package). Everything is working fine but when I tried to create a new package (let's say net.server) with the same two classes (with the package name statement in each one) I get:

Exception in thread "main" java.lang.ClassNotFoundException: MyClassName

I'm not experienced with Java and Eclipse and I have no idea what is causing this.

Foi útil?

Solução

I'd try to delete the old run configuration: from the menu select Run > Run Configurations, find your config on the left hand side, select it and click the delete button above it, finally re-run your application.

Outras dicas

You probably didn't update your imports in your main file (the one who runs the main method). ClassNotFoundException implies that the classloader couldn't find a certain class given the current directives.

Eclipse should be sending you a warning about this (underlining the offending imports in red). To organize the imports for a given file directly, use the Ctrl + Shift + O shortcut while editing it.

  • Never Recommended not to use the default package. As a standard, the root package is named after your project (e.g. If project is net.server.project, then the root package has the same name)
  • Beware that Eclipse has issues with Move refactoring (I don't know if this is fixed in Kepler 4.3)
  • Since you only have two classes, I would recommend you created the two classes from scratch, and manually copied the functionality from the old ones into the new ones. This is the safest way to ensure that everything is in order.
  • Oh, and I hope your src folder is a source folder, and not a regular one.

    Licenciado em: CC-BY-SA com atribuição
    Não afiliado a StackOverflow
    scroll top