java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast to java.lang.String
Asked Answered
E

6

19

I am getting following stacktrace error while running web application on tomcat, unable to find root cause of this exception.

Eclipse 32 bit Luna Release (4.4.0)
Tomcat 32 bit 8.0.30
jdk1.8.0_66
    Jan 01, 2016 10:02:16 AM org.apache.catalina.session.StandardManager startInternal
SEVERE: Exception loading sessions from persistent storage
java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast to java.lang.String
    at java.io.ObjectInputStream.readTypeString(ObjectInputStream.java:1419)
    at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:719)
    at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:831)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1602)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at org.apache.catalina.session.StandardSession.doReadObject(StandardSession.java:1627)
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1090)
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:261)
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:180)
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5272)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Jan 01, 2016 10:02:16 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'bgx'
Errant answered 1/1, 2016 at 4:40 Comment(0)
S
17

I appear to have had the same problem but it was not a bug in Tomcat. It was due to an object belonging to the application being stored in the HTTP Session that was not serializable and which therefore Tomcat was unable to serialize when persisting sessions.

I suggest having a gawk at the contents of Tomcat's persistent sessions storage file: SESSIONS.ser, located in the Tomcat "work" directory for the corresponding Web Application.

Opening this binary file in a Text editor (when Tomcat has shutdown!), the presence of the string java.io.NotSerializableException will identify the serialization issue and the whereabouts in the file should give a clue as to which Object/field Tomcat was unable to serialize when persisting sessions.

Soulier answered 21/2, 2017 at 11:52 Comment(5)
Well this answer is the winner. Got the same problem. Found the file, and there was my custom config class missing the most important part: "implements Serializable"Cabanatuan
To find the file with sessions, you can put a breakpoint into org.apache.catalina.session.StandardManager inside method doLoad() on the line "try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());". There you can find the current name and directory.Cabanatuan
There was a line in my tomcat log - before the exception and the stack trace - with a WARNING that gave the name of the class that couldn't be deserialized. Thanks to @Cabanatuan for the "implements Serialiazable" but in my case the class already implements Serializable - anybody got any other ideas?Paleethnology
ah, found it. It was a class of mine held inside the class mentioned in the log. i just added implements Serializable and it was donePaleethnology
Note that the error message mentioned by @Paleethnology that displays the class that cannot be serialized will appear in the console or catalina.out as a java.io.NotSerializableException during the shutdown phase. This is easy to overlook in the IDE if the console gets hidden or if your logs are cleared when restarting.Glazunov
H
4

I have had the same issue. All I did to resolve the issue isis: First stop the tomcat, then right click on it and select Clean to clean the working directory of tomcat. Then I started it again without any errors.

Hylotheism answered 25/2, 2019 at 17:8 Comment(0)
B
0

I had the same problem. Seems to be a bug in Apache Tomcat 8.0.30 and maybe versions around it.

It worked for me in version 8.0.23 and then 8.5.8 as well.

Bernie answered 1/12, 2016 at 17:12 Comment(1)
I had the same problem (github.com/orbeon/orbeon-forms/issues/6074), which was solved when upgrading from Tomcat 9.0.37 to 9.0.84.Willing
L
0

I had this same problem and the exception was the unused HttpSession variable that I declare.

Levenson answered 20/2, 2023 at 11:53 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Knightly
T
0
import java.io.Serializable;

@Service public class UserDetailsServiceImpl implements UserDetailsService, Serializable { private static final long serialVersionUID = 1L;

@Autowired
private transient UserRepository userRepository;

// Other methods...

In case of spring boot application something very similar (serializable marker interface for stored users credentials objects) could solve this issue.

Thanks

Trio answered 31/3 at 10:5 Comment(0)
T
0

For me the problem was a failing flyway migration, just needed to scroll the logs a bit up to see it.

Tse answered 17/7 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.