Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ClassNotFoundException from socket

Options
  • 06-01-2012 10:32pm
    #1
    Registered Users Posts: 1,180 ✭✭✭


    Hi, i have created an class which holds information which i would like to send over a socket to another program.
    whenever i execute this line of code:
    (customObject = (CustomObject)objectInputStream.readObject()
    
    i get a "ClassNotFoundException: <packagename> CustomObject" exception

    I have given an identical copy of the class to each runtime environment(both the client and server) and i have even made sure their serialVersionUID are the same(i even tried making them different).
    What is even more baffling is that i have implemented the same thing in the same application with another class i have created and it works perfectly

    any ideas on what i can do?


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Give more info. Could at least say you were using java or whatever too.

    You are trying to do some kind of network serialisation, read the documentation properly for the library you use.


  • Registered Users Posts: 1,180 ✭✭✭EyeSight


    srsly78 wrote: »
    Give more info. Could at least say you were using java or whatever too.

    You are trying to do some kind of network serialisation, read the documentation properly for the library you use.
    i assumed by the method call and the exception, it was implicit i was using java. but yes i am using java. and yes i have read the API documentation but i cannot figure out what is wrong.
    i know that the receiving JVM can't find the customObject class. but i'm not sure why because it has the class in the same package and in the class which receives(and deserialises) the object, i create a new customObject class and perform customObject.toString() and it works no problem

    does anyone know what's wrong?

    PS. if you need more code:
                CustomObject m = new CustomObject("1111");
                System.out.println(m.toString());
                
                //makes a socket listening
                ServerSocket serverSocket = new ServerSocket(USE_PORT);
                while(true){
                    Socket socket = serverSocket.accept();
                    
                    InputStream inputStream = socket.getInputStream();  
                    ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                    customObject = (CustomObject)objectInputStream.readObject();//EXCEPTION occurs here
                }
    


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    I'm not too familiar with Java but since you're creating an instance of CustomObject on the first line of code above shouldn't a "class not found" error occur there?
    Or is it just as simple as the variable "customObject" not being defined until it's initialised?


  • Registered Users Posts: 1,180 ✭✭✭EyeSight


    just an update if anyone wants to know. Turns out that the package of the CustomObject class was different on each virtual machine. when the method compares classes, it also compares the package of that class


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Well I don't follow that but as long as you've fixed it!


  • Advertisement
Advertisement