0

I have a class 'Data', which is defined like this :

public class Data implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = 112358L;
    public int pagenumber;
    public int chapternumber;
    public int marks;


  public  Data(int pn, int c, int m) {

       chapternumber=c;
       pagenumber=pn;
       marks=m;
        } }

I have made a .brd file (my own file type) in NETBEANS for java in windows like this :

        fout=new FileOutputStream("Data.brd");
        oos=new ObjectOutputStream(fout);
        oos.writeObject(list);

Where list is an ArrayList of Data .

I want to read this file from my android app. so I made the exact same class inside my package and saved the Data.brd in my assets folder.

ArrayList<Data> data = null; //object to be deserialized
InputStream is = null;
ObjectInputStream ois=null;
AssetManager assets = getAssets();
    is = assets.open("Data.brd");
        ois = new ObjectInputStream(is);
    data = (ArrayList<Data>) ois.readObject();

But I get Data as a blank ArrayList. Aside of that, I get the following Exceptions:

06-13 17:29:12.320: W/System.err(2180): java.lang.ClassNotFoundException: software.Data 06-13 17:29:12.320: W/System.err(2180): at java.lang.Class.classForName(Native Method) 06-13 17:29:12.324: W/System.err(2180): at java.lang.Class.forName(Class.java:217) 06-13 17:29:12.324: W/System.err(2180): at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2301) 06-13 17:29:12.324: W/System.err(2180): at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1660) 06-13 17:29:12.324: W/System.err(2180): at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683) 06-13 17:29:12.324: W/System.err(2180): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1803) 06-13 17:29:12.328: W/System.err(2180): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787) 06-13 17:29:12.328: W/System.err(2180): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003) 06-13 17:29:12.328: W/System.err(2180): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960) 06-13 17:29:12.328: W/System.err(2180): at java.util.ArrayList.readObject(ArrayList.java:657) 06-13 17:29:12.328: W/System.err(2180): at java.lang.reflect.Method.invokeNative(Native Method) 06-13 17:29:12.328: W/System.err(2180): at java.lang.reflect.Method.invoke(Method.java:511) 06-13 17:29:12.328: W/System.err(2180): at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1354) 06-13 17:29:12.328: W/System.err(2180): at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266) 06-13 17:29:12.332: W/System.err(2180): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855) 06-13 17:29:12.332: W/System.err(2180): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787) 06-13 17:29:12.332: W/System.err(2180): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003) 06-13 17:29:12.332: W/System.err(2180): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960) 06-13 17:29:12.332: W/System.err(2180): at com.mainpackage.MainActivity.onCreate(MainActivity.java:84) 06-13 17:29:12.332: W/System.err(2180): at android.app.Activity.performCreate(Activity.java:5008) 06-13 17:29:12.336: W/System.err(2180): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-13 17:29:12.336: W/System.err(2180): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 06-13 17:29:12.336: W/System.err(2180): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 06-13 17:29:12.336: W/System.err(2180): at android.app.ActivityThread.access$600(ActivityThread.java:130) 06-13 17:29:12.336: W/System.err(2180): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 06-13 17:29:12.336: W/System.err(2180): at android.os.Handler.dispatchMessage(Handler.java:99) 06-13 17:29:12.336: W/System.err(2180): at android.os.Looper.loop(Looper.java:137) 06-13 17:29:12.336: W/System.err(2180): at android.app.ActivityThread.main(ActivityThread.java:4745) 06-13 17:29:12.336: W/System.err(2180): at java.lang.reflect.Method.invokeNative(Native Method) 06-13 17:29:12.340: W/System.err(2180): at java.lang.reflect.Method.invoke(Method.java:511) 06-13 17:29:12.340: W/System.err(2180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 06-13 17:29:12.340: W/System.err(2180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 06-13 17:29:12.340: W/System.err(2180): at dalvik.system.NativeStart.main(Native Method) 06-13 17:29:12.340: W/System.err(2180): Caused by: java.lang.NoClassDefFoundError: software/Data 06-13 17:29:12.344: W/System.err(2180): ... 33 more 06-13 17:29:12.344: W/System.err(2180): Caused by: java.lang.ClassNotFoundException: software.Data 06-13 17:29:12.344: W/System.err(2180): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-13 17:29:12.344: W/System.err(2180): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-13 17:29:12.344: W/System.err(2180): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

what am I doing wrong ? I am using Eclipse IDE for the android app.

1 Answer 1

0

I get that you created the class Data in a different project. You'll have to pack that class in a jar and add it to the buildpath of your android project.

If you simply recreate the Data class in your android project it will not be the same class to the ClassLoader.

Sign up to request clarification or add additional context in comments.

5 Comments

but I have the Data.java inside the same package from where this code has been called.
Is it inside the package called software? Because a class software.Data is not the same as somepackage.Data. Anyway, I would recommend packing all classes that are shared between projects in a jar file, and adding that jar to the buildpath of the projects where those classes are needed.
the 'software' is the package i used in netbeans, but as you can see, my declaration of Data has no mention of software(thats the whole code there in Data) my package name is com.mainpackage , and this code is called by mainActivity, and the Data.java is also under the same package
The fully qualified name of a class is determined by the simple name of the class (Data) and the package it is in (software). When you deserialize an object of class software.Data Java has no way of knowing that the class is now called com.mainpackage.Data.
okay, you were right. I export the software.jar and that exception does not show anymore. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.