Due to use of Spring Batch one of my classes needs to be serializable.
This works perfectly in the Eclipse IDE. But as soon as I build the target "maven install" and test from command line, the deserialization util of spring does not find the class although a Class.forName() does find it! The class to be serialized implements Serializable
and has a serialVersionUID
.
See code example:
import org.springframework.util.SerializationUtils;
public static void main(String[] args) {
try {
Class.forName("de.test.JobConfig");
System.out.println("JobConfig found as expected");
JobConfig jc1 = new JobConfig();
jc1.setCodePage("some string");
byte[] stasis = SerializationUtils.serialize(jc1);
JobConfig jc2 = (JobConfig) SerializationUtils.deserialize(stasis);
System.out.println(jc1.equals(jc2) ? "equals" : "not equals");
} catch (ClassNotFoundException e) {
System.out.println("JobConfig not found!");
e.printStackTrace();
}
...
...
}
The result is inside Eclipse IDE:
equals
But after 'maven install'
JobConfig found as expected
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalStateException: Failed to deserialize object type
at org.springframework.util.SerializationUtils.deserialize(SerializationUtils.java:75)
at de.sopra.zeb.loader.Zeb2JiraRESTLoader.main(Zeb2JiraRESTLoader.java:75)
... 8 more
Caused by: java.lang.ClassNotFoundException: de.test.JobConfig
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.springframework.util.SerializationUtils.deserialize(SerializationUtils.java:69)
... 9 more
So, I can find the JobConfig.class in the exported JAR file even after exporting the JAR with maven install
, what the Class.forName()
proves. Why is this not the case for Spring Batch?