简体   繁体   中英

ClassNotFoundException when reading object from ObjectInputStream

I am pretty new to Spring MVC and webapps in general. I have been trying to make a project that reads an object through a socket's output stream. When I try the socket connection and my object in a separate project with just a simple main class, inputStream.readObject() works out and reads out the TradeRecord . But when I combine it in my test Springmvc project, I get a ClassNotFoundException for my TradeRecord.

Part of my controller.java class:

public void readRecord(){
    try {
        Socket socket = connectServer();
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        while (true) {
            TradeRecord rec = (TradeRecord) inputStream.readObject();
            System.out.println(inputStream.readObject());
        }
    } catch (SocketException se) {
        se.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e){
        e.printStackTrace();
    }
}

TradeRecord class which is copied directly from the person sending the object:

package models;
import java.io.Serializable;

public class TradeRecord implements Serializable
{
    public static final int STATUS = 0;
    ...
    public String TradeID;
    public char OrdStatus;
    public char TimeInForce;
    ...

    public int timeOnList = 0;
    public String venueID = "";
    public String sourceID = "";
    public String errMsg = "";
}

dispatcher:

<mvc:annotation-driven />
<context:component-scan base-package="controllers"/>
<mvc:resources mapping="/bootstrap/**" location="/bootstrap/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

and the error:

java.lang.ClassNotFoundException: TradeRecord
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:626)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1613)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)

I have tried to make the TradeRecord into a jar and manually adding it to my external libraries, under the lib of my WEB-INF, and under the lib in Tomcat (in separate occasions). I also tried to put the POJO under the base controllers package just to see if it can be found there but none of it worked. Any help would be greatly appreciated. Thanks!

edit* I am currently using Intellij IDEA 2017.1.1 and building with maven.

Tomcat can't find the TradeRecord class because it isn't there. The only 'configuration' required is to put it into the same package at source and destination and include the class in the WAR or one of its dependent JARs in its lib directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM