1

I want to deserialize an object of an image. But i'am not able to understand what error I'am getting. I have stored an image as an object in an .ser file. But when I read that file i am getting an error. Please Someone help me....

Here is my program......

package deserializeDemo;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;



import javax.imageio.ImageIO;

public class DeserializeDemo {
    @SuppressWarnings("rawtypes")
    class ImageArray extends ArrayList {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        int iHeight,iWidth;
        int[][] image;

        public ImageArray(int w,int h){
            iHeight = h;
            iWidth = w;
            image = new int [iWidth][iHeight];
        }

        public void printArray(){
            for (int[] row : image) {
                for (int col : row) {
                    System.err.println(col);
                }
            }
        }

    }




    public static void main(String [] args)
       {
        ImageArray e = null;
        ImageArray im = null;
        int iWidth=0;
        int iHeight=0;
        BufferedImage biColouredImage = null;


          try
          {
             FileInputStream fileIn = new FileInputStream("i.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             e = (ImageArray) in.readObject();
             in.close();
             fileIn.close();
          }catch(IOException i)
          {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c)
          {
             System.out.println("class not found");
             c.printStackTrace();
             return;
          }

          try {
                FileInputStream ifile = new FileInputStream("i.ser");
                ObjectInputStream ois = new ObjectInputStream(ifile);
                im = (ImageArray) ois.readObject();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            try {
             BufferedImage biColouredImage_2 = new BufferedImage(im.iWidth, im.iHeight, BufferedImage.TYPE_BYTE_BINARY);

            for (int k = 0; k < iWidth; k++) {

            for (int l = 0; l < iHeight; l++) {
             biColouredImage_2.setRGB(k, l, im.image[k][l]);
             }

             }
             File file = new File("b.png");
             ImageIO.write(biColouredImage_2, "png", file);


             } catch (IOException ioe) {
             ioe.printStackTrace();
             }


        }

}

I'am getting error as follows....

class not found
java.lang.ClassNotFoundException: javaphotosecurity.ImageArray
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at java.io.ObjectInputStream.resolveClass(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at deserializeDemo.DeserializeDemo.main(DeserializeDemo.java:56)
Piyush Mulik
  • 9
  • 1
  • 4
  • 2
    What's the error? :-) – rlegendi Sep 19 '14 at 06:50
  • Do you check the classpath of your `DeserializeDemo` if there is a `ImageArray` class? – oliholz Sep 19 '14 at 14:29
  • Looks like you changed the package of the ImageArray class after you serialized it. As per your code it should be deserializeDemo.DeserializeDemo.ImageArray – 6ton Sep 19 '14 at 14:30
  • Your question seems to essentially be "what is a ClassNotFoundException and how do I fix it?". This is covered by the existing question [What is the difference between NoClassDefFoundError and ClassNotFoundException?](http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception) – Andrzej Doyle Sep 19 '14 at 14:39

0 Answers0