Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3725366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:58:53+00:00 2026-05-19T06:58:53+00:00

I have instantized a class that implements Serializable and I am trying to stream

  • 0

I have instantized a class that implements Serializable and I am trying to stream that object like this:

try{
    Socket socket = new Socket("localhost", 8000);
    ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
    toServer.writeObject(myObject);
} catch (IOException ex) {
    System.err.println(ex);
}

All good so far right? Then I am trying to read the fields of that object like this:

//This is an inner class
class HandleClient implements Runnable{

private ObjectInputStream fromClient;
private Socket socket; // This socket was established earlier

try {
    fromClient = new ObjectInputStream(socket.getInputStream());
    GetField inputObjectFields = fromClient.readFields();

    double myFristVariable = inputObjectFields.get("myFirstVariable", 0);
    int mySecondVariable = inputObjectFields.get("mySecondVariable", 0);

    //do stuff

    } catch (IOException ex) {
        System.err.println(ex);
    } catch (ClassNotFoundException ex) {
        System.err.println(ex);
    } finally {
        try {
            fromClient.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

But I always get the error:

java.io.NotActiveException: not in call to readObject

This is my first time streaming objects instead of primitive data types, what am I doing wrong?

BONUS

When I do get this working correctly, is the ENTIRE CLASS passed with the serialized object (i.e. will I have access to the methods of the object’s class)? My reading suggests that the entire class is passed with the object, but I have been unable to use the objects methods thus far. How exactly do I call on the object’s methods?

In addition to my code above I also experimented with the readObject method, but I was probably using it wrong too because I couldn’t get it to work. Please enlighten me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T06:58:54+00:00Added an answer on May 19, 2026 at 6:58 am

    To answer your first question:

    You need to use ObjectInputStream.readObject to deserialize. You cannot read individual fields from the stream*.

    fromClient = new ObjectInputStream(socket.getInputStream());
    Object myObject = fromClient.readObject();
    

    Don’t forget to flush the output stream when writing!

    The second question is a little more complex. What the serialization mechanism does is write a class identifier to the stream followed by the serialized object data. When it deserializes it will read the class identifier and attempt to load that class (if it isn’t already loaded). It will then instantiate the object using the no-arg constructor and call the private readObject(ObjectInputStream) method. Yes, that’s right, it calls a private method from outside the class. Java serialization is special.

    If the class cannot be found (i.e. if it’s not on the classpath) then an exception will be thrown; otherwise you’ll get a fully deserialized object of the correct type assuming no other errors are found.

    For example, suppose you have the following classes:

    class Server {
        public static void main(String[] args) {
            // Set up an OutputStream sink, e.g. writing to a socket (not shown)
            ...
            ObjectOutputStream out = new ObjectOutputStream(sink);
            out.writeObject(new Data("data goes here"));
            out.flush();
            out.close();
        }
    }
    
    class Client {
        public static void main(String[] args) {
            // Set up an InputStream source (not shown)
            ...
            ObjectInputStream in = new ObjectInputStream(source);
            Data d = (Data)in.readObject();
            System.out.println(d.getData());
        }
    }
    
    class Data implements java.io.Serializable {
        private String data;
        public Data(String d) {
            data = d;
        }
        public String getData() {
            return data;
        }
    }
    

    Now suppose you put those classes into three jars (one class per jar): server.jar, client.jar and data.jar. If you run the following commands then it should all work:

    java -cp server.jar:data.jar Server
    java -cp client.jar:data.jar Client
    

    But if you do this:

    java -cp server.jar:data.jar Server
    java -cp client.jar Client
    

    then you’ll get a ClassNotFoundException because the client doesn’t know how to find the Data class.

    Long story short: the class itself is not written to the stream. If deserialization succeeds then you will have access to the object as though it had been created locally, but you will have to downcast the result of readObject to the expected type.

    There is some complexity around versioning that I’ve ignored for now. Take a look at serialVersionUID and how to deal with changes to serializable classes if versioning is likely to be an issue.

    *Not strictly true. You can call readFields inside the serializable object’s readObject method (or readResolve), but you cannot call it from outside the deserialization mechanism. Does that make sense? It’s a little hard to explain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that is a singleton. This singleton is instantiated using GCD
I have a class that I am trying to do unit tests on. The
This is a rather simple question, I have a base class which implements common
I have A WCF service that has a class that inherits System.Web.Security.RoleProvider. In this
I have a service that implements the interface LocationListener with the methods of this
I am trying to use Generic DataContract class so that I don't have to
I've got a class that implements Observable . This class is seperate to my
I have implemented a templated buffer class like this: template <class T, class Impl>
I have a Class A that when it is instantiated and saved for the
Suppose that we have previously instantiated three objects A, B, C from class D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.