3

I'm trying to make my classes Serializable. All of my classes are, but still it throws me NotSerializableException for some other classes that I can't find it's usages (e.g. com.sun.java.swing.plaf.windows.XPStyle, WClipboard).

What can I do and how can I bypass these classes when serializing or make them Serializable?

SOLVED

The problem was in LookAndFeel I have used in my JFrames and JDialogs (problem with XPStyle). Second one (WClipboard), it was used by third party class I got from here. This class uses Clipboard.

I made Clipboard field transient and LookAndFeel I couldn't manage, just by deleting it.

Aleksandar
  • 1,163
  • 22
  • 41
  • 2
    In general, classes associated with the UI (like XPStyle and WClipboard) are intentionally made not serializable because they can interface with OS specific (ie native) objects that cannot be serialized. – ControlAltDel Jun 29 '15 at 00:00
  • This often arises when you use inner classes that.are serializable and that are nested inside classes that extend Swing components. The inner class serializes the outer class along with itself, even if that wasn't your intent. Solution: static. – user207421 Jun 29 '15 at 03:28
  • Please, take a look at my comment in next answer. I know the reason but don't know solution :-) – Aleksandar Jun 29 '15 at 07:36

1 Answers1

1

If you do not want the data to be serialized for persistence or transfer, than you can declare those transient

However, if you need the data in those Objects (classes) to be persisted, transported via TCP / UDP, etc. than you might want to extend the class, and implement your own interpretation of it, so that you can then declare your extended class Serializable.


A final option might be to use a different method of Serialization, such as those provided by FST, Kryo, etc. Often times these Serialization libraries can use Reflection to Serialize Objects that the default Java implementation cannot.

  • I don't know who uses this clases at all. I I knew it, I would do advised things, but how to make it transient or extend it when I have never imported (even directly) any of these classes? – Aleksandar Jun 29 '15 at 07:29
  • 1
    Going off what you have posted, `com.sun.java.swing.plaf.windows.XPStyle` is a style, and you're more than likely applying the style at Startup for your Swing JFrame in the init() or main class. Your best option is to find and identify where this style is being set, and ensure that you're not creating an Object before setting the style. If you're creating an object, than you need to mark it transient. Otherwise, what has been said earlier is probably your best option: make it static. – Brandon Ragland Jun 29 '15 at 15:57