java.io.InvalidClassException:Serializable与Externalizable不兼容

时间:2017-08-29 10:58:28

标签: java eclipse maven serialization

一般会导致此异常的原因是什么?

我正在使用Maven开发Java项目。我需要的一个库有一个实现SerializedObjects接口的类Serializable。当我运行我的程序时,会抛出上述异常,该异常起源于类的方法readObject

奇怪的是,当我在Eclipse中手动导入库时,问题就消失了。我不认为我的pom.xml存在问题,因为从不涉及SerializedObject类的同一个库调用任何其他方法都可以正常工作。该课程有serialVersionUID,所以我不认为这是问题的根源。

我依靠mvn:package来部署我的项目,因此没有通过maven导入这个特定的库似乎不方便。过去有没有人有类似的问题?

编辑,因为我被问到一个代码片段:

public static <T> T readObject(final String inputFile) throws IOException, ClassNotFoundException {
    return SerializedObjects.<T>readObject(new InputSupplier<ObjectInputStream>() {
        @Override public ObjectInputStream getInput() throws IOException {
            return getObjectInputStream(inputFile);
        } });
}

@SuppressWarnings("unchecked")
public static <T> T readObject(InputSupplier<ObjectInputStream> inputSupplier)
        throws IOException, ClassNotFoundException {
    final ObjectInputStream input = inputSupplier.getInput();
    try{
        return (T) input.readObject(); // <-- exception here
    } finally{
        closeQuietly(input);
    }
}

异常来自return (T) input.readObject();,然后继续如下:

at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:628)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1843)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1843)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2000)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2245)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2169)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2027)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)

编辑2,我的pom.xml明确要求使用eclipse编译器:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.2</version>
            <configuration>
                <compilerId>eclipse</compilerId>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-compiler-eclipse</artifactId>
                    <version>2.8.2</version>
                </dependency>
            </dependencies>
        </plugin>

0 个答案:

没有答案