eclipse 找不到类异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5153539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:53:32  来源:igfitidea点击:

class not found exception

javaeclipsehibernatespring

提问by elle

i'm trying to run a web application in eclipse using springs and hibernate.

我正在尝试使用 springs 和 hibernate 在 Eclipse 中运行 Web 应用程序。

the dispatcher-servlet.xml is,

dispatcher-servlet.xml 是,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/test"/>
        <property name="username" value="root"/>
        <property name="password" value="mysql"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.vaannila.domain.User</value>
            </list>
        </property>


        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myUserDAO" class="com.vaannila.dao.UserDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean name="/user/*.htm" class="com.vaannila.web.UserController" >
        <property name="userDAO" ref="myUserDAO" />
    </bean>

</beans>

the error i get is,

我得到的错误是,



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.Class[]' for property 'annotatedClasses'; nested exception is java.lang.IllegalArgumentException: Cannot find class [com.vaannila.domain.User]
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:295)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:636)


The class com.vaannila.domain.Useris in the Java Resources:src folder of the project in eclipse.

该类com.vaannila.domain.User位于 eclipse 项目的 Java Resources:src 文件夹中。

采纳答案by Jigar Joshi

  1. Check that class should be there in src/com/vannila/domain/User.java

  2. check that class is annotated as shown here.and compare where you missed the thing

  1. 检查班级应该在那里 src/com/vannila/domain/User.java

  2. 检查该类是否已按此处所示进行注释。并比较您错过该内容的位置

回答by shahnaz khan

Always check the 'Problems" tab/view in eclipse. i was getting the same issue and reason was simple that i included "org.springframework.spring-library-3.1.0.RELEASE.libd" file as library when i was configuring the build path and this was in the same "dist" folder. so i just removed that and everything works fine.

始终检查 Eclipse 中的“问题”选项卡/视图。我遇到了同样的问题,原因很简单,我在配置“org.springframework.spring-library-3.1.0.RELEASE.libd”文件作为库时构建路径,这是在同一个“dist”文件夹中。所以我只是删除了它,一切正常。

回答by Jeg Bagus

i try the code, and everything is working fine.

我尝试代码,一切正常。

at first i suspect <list>will give the instance of List not array, but when i try it, it doing fine.

起初我怀疑<list>会给出 List 的实例而不是数组,但是当我尝试它时,它做得很好。

spring will throw error like you write above if they cannot find the class that you suppose to include in this case com.vaannila.domain.User

如果他们找不到您认为在这种情况下包含的类,spring 会像您上面写的那样抛出错误 com.vaannila.domain.User

check your output folder for compiled class. is there User.class exist and successfully compiled?

检查编译类的输出文件夹。是否存在 User.class 并成功编译?

回答by ArKiarX

It is easy, it is the collision happened in the library you include .please check the lib you included, in the path :

很简单,就是在你包含的库中发生了冲突。请检查你包含的库,在路径中:

   tomcat6/webapps/<you project name>/WEB-INF/lib/

you possible find two file named like : asm.jar and asm-2.x.x.jar the two jar file come from different source, one from the spring the other from hibernate,so there is some collision happened here, tomcat cannot decide which jar to added.so it report the exception.

您可能会找到两个名为 asm.jar 和 asm-2.xxjar 的文件,这两个 jar 文件来自不同的来源,一个来自 spring,另一个来自 hibernate,所以这里发生了一些冲突,tomcat 无法决定添加哪个 jar .so 它报告异常。

回答by danny.lesnik

Please create package in eclipse and add User.java to the package.

请在 eclipse 中创建包并将 User.java 添加到包中。