Is the source for the class that isn't serializable under your own control? In that case you declare it to implement Serializable. Read about Serializable in the API docs for more details.
If you have no access to the source, you are out of luck. You can't use Java serialization with classes that aren't Serializable (with the exception of a stateless superclass with a no-arg constructor which I doubt is relevant to your case if you are using it as a parameter).
Thanks for the response.
The class in question is the class returned by HttpServletResponse.getOutputStream()
.
The object returned is of type ServletOutputStream.
Can I not just subclass ServletOutputStream and make the subclass implement the serializable interface or is this not how serialization works.
It sounds like you have a big design flaw in your application.
You want to pass a ServletOutputStream from a servlet (or JSP) to an EJB, so that the EJB can write data to the output stream.
That's not a good idea.
EJB's do not necessarily run on the same server as the servlet. What would happen if you deploy your application on a cluster, and a servlet running on server A calls an EJB that happens to run on server B?
The servlet would try to pass the output stream to the EJB, and the EJB would try to write data to the output stream. But there's no way that the data that the EJB writes to the output stream gets back to the servlet container running on server A. So this design will not work.
This kind of design also violates the MVC paradigm - you normally don't implement the View part of MVC in an EJB.
So, you should reconsider the design of your application.
> Can I not just subclass ServletOutputStream and make
> the subclass implement the serializable interface or
> is this not how serialization works.
You can subclass it, and your subclass would be Serializable. But the base class part, which holds the relevant data, is not serialized. At deserialization, the base class is restored using its default constructor if it has one, and throws an exception if it hasn't.
This is explained in the API and can easily be verified with simple test code.