Eclipse - java.lang.ClassNotFoundException [英] Eclipse - java.lang.ClassNotFoundException

查看:47
本文介绍了Eclipse - java.lang.ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从 Eclipse 启动 JUnit-Test 时,我收到ClassNotFoundException".从控制台运行mvn test"时 - 一切正常.此外,Eclipse 中没有报告任何问题.

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.

我的项目结构如下:

  • 父项目(pom-packaging)
    • Web 项目(战争包装 - 我的 JUnit 测试在这里)
    • Flex 项目
    • 配置项目

    edit:怎么找不到类?这是一个简单的 HelloWorld 应用程序,没有特殊的库.

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

    这是我的 JUnit 的运行配置:替代文字 http://www.walkner.biz/_temp/runco​​nfig.png

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

    Testclass(但正如我所说;它也不适用于简单的 HelloWorld...):

    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("", ""));
        }
    }
    

    推荐答案

    这种情况我遇到过好几次,经过多次尝试,我找到了解决方案.

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

    检查您的项目构建路径并为每个文件夹启用特定的输出文件夹. 一个一个通过项目的每个源文件夹并设置 maven 将使用的输出文件夹.

    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.

    比如你的web项目的src/main/java在web项目下应该有target/classes,测试类应该有target/test-classes 也在 web 项目下等等.

    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.

    使用此配置将允许您在 eclipse 中执行单元测试.

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

    还有一个建议,如果您的 Web 项目的测试需要一些资源下的配置文件,请确保将该文件夹作为源文件夹包含并进行正确的构建路径配置.

    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.

    希望有帮助.

    这篇关于Eclipse - java.lang.ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆