ObjectOutputStream and ObjectInputStream errors

Posted on 2009-07-06
Medium Priority
Last Modified: 2013-12-22
Below are two snippets of code. The first one (starting with while(true)) is suppose to wait for a connection on a serversocket, get an ObjectOutputStream and ObjectInputStream for the socket it returns,  transmit an object of type "message", and get back the same type of object.

The second snippet is suppose to connect to the program running the first snippet, get a message, and transmit back a message. These two parts are suppose to work as mirror images of each other, IE the output stream of one is the input stream of the other.

The flow is as follows:
1 First snippet runs, waits for accept.
2 Second snippet runs, connects to the port where the first waits
3 First accepts, opens a socket, gets an ObjectOutputStream and sends a message
4 Second gets message from its ObjectInputStream (the other end of the first's output) and transmits back a message on its outputstream
5 First gets message from second on its InputStream which is the other end of the seconds's output stream

Basically, this is a handshake senario, however, when I run both programs, the first snippet gives the following error:
      at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
      at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
      at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
      at dimes.comm2gui.GUIConnectorThread.run(GUIConnectorBean.java:165)
(line 165 is marked with ---> in the code below)

On the other side of the socket, the second snippet (which is suppose to receive the message after it connects)  gives this error:
java.lang.ClassNotFoundException: dimes.comm2gui.common.Message
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:247)
      at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
      at dimes.comm2agent.AgentConnectorBean.initConenction(AgentConnectorBean.java:86)
(Line 86 is marked with a --->)

I assume both of these problems are part of the same basic issue. But I don't know what it is. From what I've been reading the trouble seems to be with the Object Header not being read completely or something of that nature. I attempted to flush the Outputstream after I start it, in the hope that it may transmit the header, but that didn't seem to work.

Any ideas?

private ServerSocket comm;
	private Socket lComm;
	private GUIConnectorBean bean;
	private String xml;
	private ObjectOutputStream oOStream;
	private ObjectInputStream iOStream;
	private Message Current;
	private GUICommunicator Communicator;
			try {
				lComm=comm.accept(); //Wait for connection from GUI
				oOStream = new ObjectOutputStream(lComm.getOutputStream());
				Message msg = new Message(MessageTypes.DIMESACK, -1, "DIMES", new Object[]{bean.getAgentVersion()});
				InputStream iStream = lComm.getInputStream();
----->			iOStream = new ObjectInputStream(iStream);
				Current = (Message)iOStream.readObject();
				if (MessageTypes.DIMESACK==Current.getType());
				else continue;
				Communicator = GUICommunicator.getInstance();
			} catch (IOException e) {
				// TODO Auto-generated catch block
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
	private Socket commSocket = new Socket();
	private InetAddress localHost;
	private Logger logger;
	private Thread iP;
	private ObjectInputStream iOStream;
	private ObjectOutputStream oOStream;
				commSocket.connect(new InetSocketAddress(localHost, CONNECT_PORT),SOCKET_TIMEOUT);
				iOStream = new ObjectInputStream(commSocket.getInputStream());
--->				Message msg = (Message) iOStream.readObject();
				if (!(MessageTypes.DIMESACK==msg.getType())) return false;
				inputString = msg.getAttrib();
oOStream = new ObjectOutputStream(commSocket.getOutputStream());
					oOStream.writeObject(new Message(MessageTypes.DIMESACK, -1, "", null)); //Send an empty ACK message to Agent to finish handshake
					iP = new Thread (new inputProcessor(iOStream, logger)); 
					return true;

Open in new window

Question by:bored_shiva
LVL 16

Accepted Solution

imladris earned 2000 total points
ID: 24785985
The critical error looks to be the ClassNotFound exception for Message. This causes the receiver to not run at all; which causes the sender to shutdown with an End Of File.

A ClassNotFound exception is an indication that the JVM can't find the class it needs; Message in this case. The JVM looks at each directory on the classpath. The classpath is specified by an environment variable, or as a commandline argument. Also, the JVM shows the Message class as being in a package:


so it will be looking at each directory in the class path, and then for subdirectories of dimes\comm2gui\common and hoping to find a Message.class file there. The exception indicates that it has failed to find the needed class.

Author Closing Comment

ID: 31600123
I actually ended up finding this out myself, but you're absolutely right. I had a copy of the class in a different location, with the proper include and all, but still apparently not enough to satisfy the JVM. Once I changed the location to be identical in both places, the program(s) worked like a charm.  

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question