Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - java.lang.ClassNotFoundException

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("", ""));     } } 
like image 375
swalkner Avatar asked Jun 27 '09 15:06

swalkner


People also ask

What causes java Lang ClassNotFoundException?

The java. lang. ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. The Java ClassNotFoundException is a checked exception and thus, must be declared in a method or constructor's throws clause.

What is ClassNotFoundException in java?

ClassNotFoundException is a checked exception and occurs when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. In older days, there are no editors like Eclipse are available.


1 Answers

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.

like image 151
Carlos Avatar answered Oct 03 '22 21:10

Carlos