Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,374 Java Programmers. There are 1,414 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Serialization help

 
Reply to this topicStart new topic

Serialization help, ClassNotFound Error

sleepy_eyes
post 7 May, 2008 - 10:52 AM
Post #1


New D.I.C Head

*
Joined: 5 Oct, 2007
Posts: 7



The file to be read is called "Instructions.ser", and there is a readable text 'serialization.SerializedObject' at the beginning of the file but everything else is serialized. I've read in the serialized file and data, but when I run it, I get an error:

CODE

java.lang.ClassNotFoundException: serialization.SerializedObject
        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:289)
        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 lab10svang6.Main.main(Main.java:43)


I'm at my wits end trying to find out what the problem is. Can anyone help me? I'm thinking it has to do with the serialization.SerializedObject but am not too sure.

Here is the code:
CODE

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Main
{


    public static void main(String[] args)
    {
        //read file
        FileInputStream fis = null;
        try
        {
            //read in file
            fis = new FileInputStream("Instructions.ser");
            ObjectInputStream input = new ObjectInputStream(fis);
            ArrayList<SerializedObject> aliSO = new ArrayList<SerializedObject>();
            SerializedObject instruction;
            
            //read data
            try
            {
                while( true )
                {
                    instruction = (SerializedObject) input.readObject();
                    aliSO.add(instruction);

                    for( int i = 0; i <= aliSO.size(); i++ )
                    {
                        for( SerializedObject so : aliSO )
                        {
                            if( i == so.getIntValue() )
                            {
                                System.out.print(so);
                            }
                        }
                    }
                }
            } //end try
            //close file
            catch( EOFException ex )
            {
                input.close();
            }
        }
        catch( FileNotFoundException ex )
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("File not found.");
        }
        catch( IOException ex )
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Error opening file.");
        }
        catch( ClassNotFoundException ex )
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            try
            {
                fis.close();
            }
            catch( IOException ex )
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Error closing file.");
            }
        }


    }
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 7 May, 2008 - 12:24 PM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(sleepy_eyes @ 7 May, 2008 - 10:52 AM) *

The file to be read is called "Instructions.ser", and there is a readable text 'serialization.SerializedObject' at the beginning of the file but everything else is serialized. I've read in the serialized file and data, but when I run it, I get an error:


Are you telling me that you a file which has the ASCII String
"'serialization.SerializedObject"
followed by serialized Java Objects ?

The program who created it had to do tricky things... writing the String bytes using and outputStream then still using the same outputStream writing the serialized bytes of the objects.

Just change the program which created the file to write as a String object the header and then the other objects. Than you can

String header = (String) input.readObject();
and then proceed with your while loop
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

sleepy_eyes
post 8 May, 2008 - 09:20 AM
Post #3


New D.I.C Head

*
Joined: 5 Oct, 2007
Posts: 7

The thing is, I don't have the program which created the file. I was just given the file.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 12:34 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(sleepy_eyes @ 8 May, 2008 - 09:20 AM) *

The thing is, I don't have the program which created the file. I was just given the file.

Are you sure the first line is NOT a serialized String ?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

sleepy_eyes
post 10 May, 2008 - 06:16 AM
Post #5


New D.I.C Head

*
Joined: 5 Oct, 2007
Posts: 7

QUOTE(pbl @ 8 May, 2008 - 12:34 PM) *

QUOTE(sleepy_eyes @ 8 May, 2008 - 09:20 AM) *

The thing is, I don't have the program which created the file. I was just given the file.

Are you sure the first line is NOT a serialized String ?


The first line is a serialized String, but I've figured what the problem was: Serialized object can only be read by the program that created it, thus serialized data cannot be shared between systems. So I guess in order to completely get it working we needed the program from the instructor. (I think.)
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 10 May, 2008 - 04:49 PM
Post #6


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

QUOTE(sleepy_eyes @ 10 May, 2008 - 06:16 AM) *

The first line is a serialized String, but I've figured what the problem was: Serialized object can only be read by the program that created it, thus serialized data cannot be shared between systems. So I guess in order to completely get it working we needed the program from the instructor. (I think.)


You are completly WRONG... serializabled things is there to SHARE between different applications on different systems.

So read your "header" as a String and the rest as SerialiazedObject
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

sleepy_eyes
post 11 May, 2008 - 08:12 PM
Post #7


New D.I.C Head

*
Joined: 5 Oct, 2007
Posts: 7

Thanks.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 02:48AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month