Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization & Predefined Primitive Classes  RSS feed

 
Matthew Porter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to serialize and deserialize one of the predefined primitive classes (i.e. Integer.TYPE, Boolean.TYPE, etc.). Serialization seems to work without any issues; however, when deserializing, a ClassNotFoundException: int is thrown. Can anyone explain why and (hopefully) how to work around this? Thanks in advance.
-matthew porter
 
Snigdha Solanki
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be better if you can post your code.
 
parag Chatterjee
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though u r serializing a class file a copy of that is required to be on the client side also.
As you have not kept the class file on client side yu r getting class not found exception.
 
Matthew Porter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. So here is a snippet of code that demonstrates this problem. When running, you need to pass a file name to serialize to. Thanks.

-matthew

CODE SNIPPET
-----------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class PrimitiveSerialization {
public static void main(String[] args) {
Object primitiveClass = Integer.TYPE;
if (args.length > 0) {
File file = new File(args[0]);
try {
serializeObjectToFile(file, primitiveClass);
Object retrievedPrimitiveClass = serializeObjectFromFile(file);
System.out.println("retrievedPrimitiveClass.equals(primitiveClass) = " + retrievedPrimitiveClass.equals(primitiveClass));
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Please provide a file name.");
}
}
private static void serializeObjectToFile(File file, Object primitiveClass) throws Exception {
FileOutputStream fileOS = new FileOutputStream(file);
ObjectOutputStream objOS = new ObjectOutputStream(fileOS);
objOS.writeObject(primitiveClass);
objOS.flush();
fileOS.flush();
objOS.close();
fileOS.close();
}
private static Object serializeObjectFromFile(File file) throws Exception {
Object serializedObject = null;
FileInputStream fileIS = new FileInputStream(file);
ObjectInputStream objIS = new ObjectInputStream(fileIS);
serializedObject = objIS.readObject();
objIS.close();
fileIS.close();
return serializedObject;
}
}
 
Erick Jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a known bug in the JDK and is fixed in 1.4. There is a workaround posted in the bug report, although I have not tried it.
http://developer.java.sun.com/developer/bugParade/bugs/4171142.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!