The Artima Developer Community
Sponsored Link

Java Answers Forum
class not found exceptions and serialization

1 reply on 1 page. Most recent reply: Dec 22, 2003 3:22 PM by Jeff Woods

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jeff Woods

Posts: 2
Nickname: jw
Registered: Dec, 2003

class not found exceptions and serialization Posted: Dec 19, 2003 10:56 AM
Reply to this message Reply
Advertisement
I am having trouble de-serializing Objects. I keep getting "class not found" exceptions when I compile certain programs that I've written. Others, using the same imports & in the same folder compile just fine & I can't see what the problem is. I have a pretty good knowledge of Java but this one has me stumped. I've spent the last 2 days on the Internet, found lots of good stuff, but I'm still baffled as to why I'm getting these errors. I understand that the class files have to be available to de-serialize, but when it won't compile, no class files obviously are generated!! It is not difficult code & appears to be the standard way to write/read Objects. Somewhere, my Object "signatures" must be getting lost/changed whatever. A couple of examples follow. All of the files are in a folder c:\java\scratch. The client compiles, the server throws a "class not found" exception & won't compile (hopefully no brackets/semicolons were lost during copy/paste). If anyone has any suggestions of sees what's wrong, I'd appreciate some feedback! Thanks...

import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ClientTest implements Serializable
{
public ClientTest(){}

public static void main(String[] args)
{
try
{
Hashtable settings = new Hashtable();
settings.put("buffers", new Integer(20));
settings.put("layers", new Integer(2));
settings.put("title", "navigator");

InetAddress ia = InetAddress.getByName("localhost");
Socket socket = new Socket(ia, 6789);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(dos); //socket.getOutputStream()

oos.writeObject(settings); // new Date()
oos.flush();
oos.close();
socket.close();
}
catch(IOException e)
{
System.err.println("Threw an IOException!" + e);
}
}
}

import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ServerTest implements Serializable
{
public ServerTest()
{
}
public static void main(String[] args)
{
new ServerTest();
try
{
ServerSocket listener = new ServerSocket(6789);
while(true)
{
Socket socket = listener.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
ObjectInputStream ois = new ObjectInputStream(dis);

//Date date = (Date)ois.readObject();
Hashtable settings = (Hashtable)ois.readObject();
System.out.println("Got object: " + settings.toString()); // settings

ois.close();
dis.close();
socket.close();
}
}
catch(IOException e)
{
System.err.println("Threw an IOException!" + e);
}
}
}


Jeff Woods

Posts: 2
Nickname: jw
Registered: Dec, 2003

solved my own problem... Posted: Dec 22, 2003 3:22 PM
Reply to this message Reply
After days of surfing & not figuring out what the problem was I stumbled upon it today. It seems that if you use catch(IOException + e) in a try/catch block when writing/reading objects instead of (Exception + e) (or maybe some other error reporting routine), it will throw a "class not found" exception. That's about the only thing I never considered changing in my code!!! I'm sure the API contains the reason why, but for now, I'm just glad I figured it out, quite by accident. Maybe this will help other people in the future!!!
-Jeff

Flat View: This topic has 1 reply on 1 page
Topic: FileOutput Previous Topic   Next Topic Topic: Entity Expansion in JDOM Parser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use