Org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException

I am using org.apache.commons.lang.SerializationUtils, but I have an error. I am new to Java, if you need more information, please let me know the code:

Profile profile2 = new Profile();
profile2.setFileName(path);
profile2.setStatus("UPLOADED");
byte[] payload2 = SerializationUtils.serialize(profile2);
profile = (Profile) SerializationUtils.deserialize(payload2);

runtime error output:

org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException: com.xxx.xxx.Profile
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:166)
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:193)

people say the profile is not in the classpath. if that were true, an error occurred in "new Profile ()". I'm right?

I just found a workaround:

profile = (EveSuccessCriteriaProfile) SerializationUtils.deserialize(payload2);

replaced by

InputStream fis = null;
fis = new ByteArrayInputStream(payload2);
ObjectInputStream o = new ObjectInputStream(fis);
profile = (Profile) o.readObject();

it works great

+4
source share
1 answer

, OSGi , . . , Profile.class.getClassLoader(), , Profile. SerializationUtils.class.getClassLoder(), commons.lang. commons.lang Import-Package Profile, . , SerializationUtils.deserialize Profile, .

, ThreadContext .    Thread.currentThread().setContextClassLoader(Profile.class.getClassLoader()); SerializationUtils.deserialize.

. , ClassLoader . habbit, ContextClassLoader .

, SerializationUtils OSGi. : https://issues.apache.org/jira/browse/LANG-1049

https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ClassLoaderObjectInputStream.html, .

+5

Source: https://habr.com/ru/post/1613597/


All Articles