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

  • Home
  • SEARCH
  • 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 8960149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:36:29+00:00 2026-06-15T15:36:29+00:00

In Java, Serialization makes reading and writing objects to streams REALLY easy. For instance,

  • 0

In Java, Serialization makes reading and writing objects to streams REALLY easy. For instance, the following code snippet is mostly all it takes to write objects to a stream:

ObjectOutputStream oos = ... //Initialize your output stream
Object toWrite = ...         //Initialize what you want to write here
oos.writeObject(toWrite);    //Writes the object to the stream
oos.flush();

This will work just fine, provided that toWrite‘s class implements the Serializable interface, AND that all of toWrite‘s non-transient member variables are also Serializable. In other words, the entire Object hierarchy you are trying to send via the toWrite reference must be Serializable. Assume that the only problem with this code is that something inside toWrite isn’t Serializable.

If the hierarchy isn’t completely Serializable, the call to oos.writeObject(toWrite) throws a java.io.NotSerializableException. This is all fine and good, except that the Exception doesn’t give you quite everything you need to fix the problem quickly. It will tell you what class couldn’t be serialized. Your stack trace might look something like this:

java.io.NotSerializableException: some.package.and.ClassIDidntExplicitlyReference
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    ...
    at my.package.MyClass.codeThatWroteTheOffendingObject(MyClass.java:###)

This sort of points you in the right direction, but in cases that involve deep Object hierarchies referenced by toWrite, it’s not always clear where the offending class reference came from. Let’s say I assign toWrite to an instance of MyClass and my instance of MyClass has a member Object reference called nonSerializableReference that has been set to an instance of ClassIDidntExplicitlyReference, which is not Serializable. I’d like to see something like the following printed out:

my.package.MyClass.nonSerializableReference instanceof some.package.and.ClassIDidntExplicitlyReference

I know this problem probably doesn’t have a quick solution, and will likely involve using Reflections. Has anyone here on SO done this before and if so, would you mind sharing your insight?

Answer

I ended up using Reflection’s Field and Modifier class to find the path. Unfortunately, I can’t share my code (against company policy), but I can show you my output. Class A holds an instance of B, B holds an instance of C, and C holds an instance of D. All are Serializable except for D. I ended up using Depth-First-Search to find the path:

A.b -> 
  B.c -> 
    C.d instanceof class D

If I make C.d transient, the search yields no results. Pretty cool!

  • 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-06-15T15:36:30+00:00Added an answer on June 15, 2026 at 3:36 pm

    This is a strange question – you’re wanting to determine at runtime what you should be doing at compile time. In theory, the path to the object shouldn’t matter – you need to find the objects that reference things that aren’t serializable to fix your code.

    That said, you could write a simple recursive tree crawler using reflection. That said, you might be able to get away with implementing your own ObjectInputStream that logs the objects coming in appropriately. I recommend taking a look at the source for more detail

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

Sidebar

Related Questions

I am reading some materials on Java serialization and found that, for self defined
Can anyone tell me what is the need of Serialization of objects in Java
In Java it is very easy to serialize objects. In C++ it is only
In a java serialization problem, I want to save some classes name and I
In a process of leaning java serialization concept, i was puzzled at one point.
I have a client and server communicating via Spring remoting (using Java Serialization) over
What is the difference between serialization and synchronization in java? I need an explanation
I'm reading about program specialization - specifically java and I don't think I quite
Kryo is a very new and interesting Java serialization library, and one of the
What is the best procedure for storing and retrieving, using native Java serialization, generic

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.