Gen Classic Java Proxy "java.io.NotSerializableException"
search cancel

Gen Classic Java Proxy "java.io.NotSerializableException"

book

Article ID: 259285

calendar_today

Updated On:

Products

Gen Gen - Host Encyclopedia Gen - Run Time Distributed Gen - Workstation Toolset

Issue/Introduction

The Classic Java Proxy ABEAN class (and its operation class) are declared as Serializable. e.g. for PStep S1 packaged in Server Manager Svr1 the generated classic java proxy code (...\proxy\java\src\S1\Abean) shows:

===
Svr1.java:
public class Svr1  implements ActionListener, java.io.Serializable  {
===

===
Svr1Operation.java:
public class Svr1Operation
         implements Serializable
{
===


However when trying to test serialization using an "ObjectOutputStream" encounter a runtime error.
Code used for s1test.java
===
import java.util.*;
import java.io.*;

public class s1test extends Thread {


    private static Exception exception = null;

    private static class operListener implements java.awt.event.ActionListener, java.io.Serializable {
        operListener() {}
        public void actionPerformed(java.awt.event.ActionEvent aEvent) {
            exception = new Exception(aEvent.getActionCommand());
        }
    }

    public static void main(String[] main) throws Exception{

        // Tsvblg.Abean.EsuSTasTnCsvValues op  = new Tsvblg.Abean.EsuSTasTnCsvValues();
        S1.Abean.Svr1 op = new S1.Abean.Svr1();
        op.setTracing(0);
        op.addExceptionListener(new operListener());

        op.clear();
        exception = null;
        op.setComCfg("TCP localhost 2008");
        StringWriter w = new StringWriter();
        op.execute();

        FileOutputStream os = new FileOutputStream("cxcxcxcxcx");
        ObjectOutputStream o = new ObjectOutputStream(os);
        o.writeObject(op);
        o.close();
        os.close();

        System.out.println(exception + " " + w );
    }
}
===

Error at s1test.class runtime:
===
Exception in thread "main" java.io.NotSerializableException: sun.misc.Launcher$AppClassLoader
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
        at s1test.main(s1test.java:31)
===

Environment

Release : 8.6

Cause

Even though a class implements the Serializable interface, that does not necessarily mean the class is actually serializable.
If the class contains an object that is not serializable, then the NotSerializableException will be thrown at runtime when/if the class is attempted to be serialized.
The Classic Java Proxy's ABean Operation class contains an ITranEntry object that contains a ClassLoader object. Since the ClassLoader object is not serializable, the runtime exception is thrown.

Resolution

The Classic Java Proxy was replaced by the Standard/New Java Proxy several releases ago due to multiple limitations (serialization being one of them) in the Classic Java Proxy. 
The intention was to deprecate the Classic Java Proxy, but it was too widely used so the decision was made to not deprecate it, but to also not enhance it to avoid the risk of breaking any existing applications.
Any new development should be done using the Standard/New Java Proxy interface which supports Java serialization.
The Standard Java proxy interface documentation can be found here and includes a list of improvements over the Classic version under the "View Objects" section:
Gen™ 8.6 > Distributed Processing > Working With Proxies > Java Proxy > Java Proxy Interface

 

Additional Information

If instead of writing out the entire Classic Java Proxy object to the "ObjectOutputStream" just the views were written out then it may be successful.