Search code examples
javaserializationstreamdata-transfer-objects

Java: Transferable Objects & Serialization


I need to serialize a Transferable object to I can send it over an object data stream but during runtime I get the error java.io.NotSerializableException & I have no idea whats wrong. How do I fix this?

Here's the part of the code that is causing the error

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    System.out.println(contents);

    //Initialiaze ObjectStreams
    FileOutputStream fos = new FileOutputStream("t.tmp");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    //write objects
    oos.writeObject(contents);
    oos.close();

Solution

  • The vest way around this is to parse each data flavor into a serializable object of it's kind i.e. put string clipboard content into a string object