Java – serialization [replication] of lambda

See English answer > unable to deserialize lambda 2

I have two completely separate projects that contain a class

Item 1:

class TestMain {

    public static void main(String[] args) {
        Runnable r = (Runnable & Serializable) () -> {};

        // Serialize r to C:/file.ser;
    }
}

Item 2:

class TestMain2 {

    public static void main(String[] args) {
        // Deserialize C:/file.ser to runnable;
    }
}

However, when trying to deserialize runnable, it throws an exception saying that it cannot find testmain

... is there any way to avoid this situation?

Solution

The solution is to include testmain in the classpath when deserializing

Lambda implicitly relies on the external class that declares it

In addition, Java Tutorial says:

Except... As @ Tim pointed out... Even if you don't need testmain Class file, you still need testmain $XXX containing lambda code Class file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>