如何獲取更多關於序列化異常的堆疊資訊
阿新 • • 發佈:2018-12-04
問題與解決方法
有時候我們的系統會報java.io.NotSerializableException
,根據堆疊資訊又無法得出有用的資訊,無法找到具體是因為哪些類或者變數造成的,可以使用一個java引數-Dsun.io.serialization.extendedDebugInfo=true
來得到更加具體的序列化資訊。
從jdk6開始就可以使用該引數,可以在啟動java程式時啟用該引數,也可以在專案的一開始通過程式碼來啟用:
System.setProperty("sun.io.serialization.extendedDebugInfo", "true");
下邊是stackoverflow上找到的兩個回答:
回答其一
From JDK6 you should be able to get extra information by setting the sun.io.serialization.extendedDebugInfo system property:
-Dsun.io.serialization.extendedDebugInfo=true
回答其二
Set the system property
sun.io.serialization.extendedDebugInfo
totrue
, either by adding-Dsun.io.serialization.extendedDebugInfo=true
to the command line, or add the lineSystem.setProperty("sun.io.serialization.extendedDebugInfo", "true");
at the start of your program.
If something isn't serializable, this will cause a trace of the path through the data structure that leads from the "root" object (the one passed to ObjectOutputStream.writeObject()) to the object that's not serializable. At least, it'll tell you the class names of the instances and the fields that lead to the non-serializable object.