Eclipse - java.lang.ClassNotFoundException

370,356

Solution 1

I've come across that situation several times and, after a lot of attempts, I found the solution.

Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use.

For example, your web project's src/main/java should have target/classes under the web project, test classes should have target/test-classes also under the web project and so.

Using this configuration will allow you to execute unit tests in eclipse.

Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.

Hope it helps.

Solution 2

Carlos approach helped! Eclipse - java.lang.ClassNotFoundException

Try to check the classpath of the junit run configuration:

  1. Open your run configurations
  2. Click on the jUnit-Test you want to start
  3. go to the classpath tab
  4. Try to add a folder (click on user entries, click on advanced, click on add folders, click on ok and search the outputfolder for your test classes(those you find under projektproperties java build path, source))

works for me.

Solution 3

your build classpath is correct, which is why you can compile. the classpath for your JUnit needs to be checked. go to the Run menu and choose 'open run dialog.' in there you should see a tree on the left with JUnit as an option. open that node and find and select your test. on the right pane you will see a tab for classpath. take a look to ensure that your class that the test is trying to instantiate would be found.

edit:

this seems to be an issue with maven and its behavior after a release changed the default Eclipse output folders. i have seen solutions described where

  • placing maven into the bootclasspath ABOVE the jre works, or
  • running mvn clean test does the trick or
  • refreshing all of your eclipse projects, causing a rebuild fixes the problem
  • going to your project and selecting Maven->Update Configuration solve the problem

with the first three, there were reports of the issue recurring. the last looks best to me, but if it doesnt work, please try the others.

here and here is some info

Solution 4

Enabling [x] Use temporary JAR to specify classpath (to avoid classpath length limitations) inside the Classpath tab of the Run configuration did the trick for me.

If your project is huge and you have lots of dependencies from other sibling projects and maven dependencies, you might hit the classpath length limitations and this seems to be the only solution (apart from making the directory to you local maven repo shorter (ours already starts at c:/m2)

enter image description here

Solution 5

The problem might be missing the class file in your build folder. One solution is clean the project and rebuild it.

Share:
370,356

Related videos on Youtube

swalkner
Author by

swalkner

Updated on February 07, 2022

Comments

  • swalkner
    swalkner about 2 years

    When trying to start my JUnit-Test out of Eclipse, I get a "ClassNotFoundException". When running "mvn test" from console - everything works fine. Also, there are no problems reported in Eclipse.

    My project structure is the following:

    • parent project (pom-packaging)
      • Web project (war-packaging - my JUnit-test is in here)
      • Flex project
      • Configuration project

    edit: How can the class not be found? It's a simple HelloWorld-Application with no special libraries.

    Here's my JUnit's run-configuration: alt text http://www.walkner.biz/_temp/runconfig.png


    Testclass (but as I said; it doesn't work with a simple HelloWorld either...):

    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import biz.prognoserechnung.domain.User;
    import biz.prognoserechnung.domain.UserRepository;
    import biz.prognoserechnung.domain.hibernate.UserHibernateDao;
    
    public class UserDaoTest {
    /**
     * the applicationcontext.
     */
    private ApplicationContext ctx = null;
    
    /**
     * the user itself.
     */
    private User record = null;
    
    /**
     * Interface for the user.
     */
    private UserRepository dao = null;
    
    @Before
    public void setUp() throws Exception {
    String[] paths = { "WEB-INF/applicationContext.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
    dao = (UserHibernateDao) ctx.getBean("userRepository");
    }
    
    @After
    public void tearDown() throws Exception {
    dao = null;
    }
    
    @Test
    public final void testIsUser() throws Exception {
    Assert.assertTrue(dao.isUser("John", "Doe"));
    }
    
    @Test
        public final void testIsNoUser() throws Exception {
        Assert.assertFalse(dao.isUser("not", "existing"));
            Assert.assertFalse(dao.isUser(null, null));
            Assert.assertFalse(dao.isUser("", ""));
        }
    }
    
    • swalkner
      swalkner almost 15 years
      I get the same error when trying to run a simple HelloWorld class with no JUnit included... More exactly: java.lang.NoClassDefFoundError: HelloWorld Caused by: java.lang.ClassNotFoundException: HelloWorld
    • akf
      akf almost 15 years
      can you show the code for your test class?
    • Jonathan Holloway
      Jonathan Holloway almost 15 years
      do you have build automatically turned on or off?
    • swalkner
      swalkner almost 15 years
      build automatically is turned on
    • Admin
      Admin over 12 years
      Thanks a lot to Carlos.My problem is solved. Link to carlos post : <stackoverflow.com/questions/1052978/…>
    • Admin
      Admin over 12 years
      Thanks a lot to Carlos.My problem is solved. Link to carlos post : stackoverflow.com/questions/1052978/…
    • Soudamini
      Soudamini about 7 years
      First of all avoid creating a .java class with a main method in the project. If its created the project will start its execution from main and if its deleted in future the project wont work well. If so happens kindly go to run-> run configurations-> java-> right click on main class-> delete the class. now your project is ready to run.
  • swalkner
    swalkner almost 15 years
    thanks a lot for your answer, but nevertheless: it doesn't work either; first, I have to include spring-test.jar, then another dependency is not fullfilled. i don't understand why it doesn't work with a simple helloworld-class in the same package. and there, i don't need a "hack" like the one suggested by you... something must be wrong with "classpath" or something similar, but i don't get where/what i have to check...
  • swalkner
    swalkner almost 15 years
    Hi Carlos, thanks a lot for your advice. But it didn't help, unfortunately. I added /src/main/java (target/classes) and /src/test/java (target/test-classes) to the source folders as well as src/main/resources (applicationContext.xml, some properties - target/classes). Same problem - when starting my src/test/java/UserDaoTest.java as JUnit-test from Eclipse, I get "Class not found UserDaoTest java.lang.ClassNotFoundException: UserDaoTest". At least, that's a little bit different then the original error message...
  • swalkner
    swalkner almost 15 years
    unfortunately, "Maven => Update Configuration" does not solve the problem at all... the reverse is true; it delete's the build path configurations... Or does that mean that some other configuration is bad?
  • Carlos
    Carlos almost 15 years
    You're on the right way, check whether the UserDaoTest class is in the corresponding output folder within your file system (As you may know, Eclipse does compile all your source files and place it into the specified output folder, among others, to be able to launch tests). If the class doesn't appear in the output folder, check the inclusion/exclusion filters of the build path for this specific folder. Good luck.
  • Jose Muanis
    Jose Muanis over 12 years
    Isn't this very strange? It worked for me, but the target classes dir is part of the build classpath for the project. Doesn't make much sense that you need to add a specific class.
  • awied
    awied about 12 years
    I don't think this action necessarily adds a needed directory as much as triggers something to be reloaded. You can likely immediately remove the newly added directory after a successful run and it should work like normal. Also, comment above is right. May want to upvote the referenced commend from Carlos as well.
  • Stefan Haberl
    Stefan Haberl almost 12 years
    Exactly as @swalkner pointed out: Maven => Update configuration will ruin the build path configuration. For some reason which eludes me the plugin will set exclusion filters which I always have to remove manually after running the command
  • Stefan Haberl
    Stefan Haberl almost 12 years
    For me this didn't fix the problem. Turned out that it was an AspectJ issue. If you're using AspectJ try removing and reading the AspectJ capabilities - worked for me
  • bgs
    bgs over 11 years
    Had same issue. I think it is an eclipse project - maven - git issue. That does not really narrow it down but wiping all the metadata folders (.project, .whatever...), deleting the project and reimporting only as a maven project worked for me. I also found something didn't know eclipse-junit could do: highlighting test method and selecting RunAs-Junit only runs that particular test-method, not the entire test! No more @Ignore all over the place and gui shortcuts!
  • ArtOfWarfare
    ArtOfWarfare over 10 years
    I found the answer on another SO question to be more helpful with this problem: stackoverflow.com/a/5718520/901641
  • justin
    justin over 9 years
    After too much time wasted I ended up doing the steps above. I then took a heroic decision to delete all run test configurations and now none of my tests can be found in the classpath even though the new run configuration clearly shows the project in it (with maven dependencies underneath). I keep forgetting how brittle eclipse is. After that no tests could be run with eclipse (before i could select a particular test - ok). Finally, I tried KomodoDave suggestion below. That fixed things... amazing the loops you have to jump through with this ide (the latest of a series..)
  • Berit Larsen
    Berit Larsen over 8 years
    "go to the Run menu and choose 'open run dialog.' Huh?? Where's that?
  • CoffeDeveloper
    CoffeDeveloper over 8 years
    I always runned junit test cases, until I runned the program that checks PC for compatibility before upgrading windows. That seemeed to break something, I'm unable to run JUnit test cases again. (and all existing answers don't work)
  • Amos M. Carpenter
    Amos M. Carpenter over 8 years
    I'm tempted to ask whether it's fixed your problem permanently, or every time... but I get what you mean, and, amazingly, this did the trick for me.
  • Carl Poole
    Carl Poole about 7 years
    What if the external jar file is important to the application and necessary for some functionality of the app? I'm not sure this is a viable solution.
  • Nutan
    Nutan almost 4 years
    Was facing the same issue, I simply used Eclipse -> Project -> clean and it worked. Mvn clean did not work.