Invoking ObjectInputStream.readObject(); method in java is throwing ClassNotFoundException when called from thread run();

StackOverflow https://stackoverflow.com/questions/22155957

  •  19-10-2022
  •  | 
  •  

Question

I'm trying to implement a thread in tomcat. Basically it's for handling Java server socket protocols. The thread works fine outside tomcat application but when I try to invoke another class and it's methods using this thread run() it gives me java.lang.ClassNotFoundException: com.app.util.Data Here is the full version of errors:

java.lang.ClassNotFoundException:  com.app.util.Data
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at com.app.util.pack.ServerClientHandler.run(ServerClientHandler.java:52)

The class file(com.app.util.Data) is correctly placed in the specified path and imported within com.app.util.pack.ServerClientHandler. I know there a lot of questions asked about ClassNotFoundException error, but in my situation the file is placed correctly in the path and there are absolutely no errors thrown by the compiler or IDE. Is there any specific reason why the ClassNotFoundException is thrown for an available class file by a thread run() method in a tomcat server and is there any alternative way to use thread which will work fine in tomcat?

I know this is totally a noob question without any leads, but I didn't find any answer online. Thanks again!


Updates:

Guy, I have solved the problem. actually it was not the problem with the run() method. ObjectInputStream, readObject() was giving me the error. i just had to keep a identical package name in both server and client and of course serialize it. Well it's solved now. thank you all for your help :) thanks a lot!

No correct solution

OTHER TIPS

Remember you can't call classes loaded by the application classloader from classes loaded by the server classloader.

You can read a good explanation here:

http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top