【发布时间】:2015-10-24 08:19:46
【问题描述】:
我收到了java.lang.ClassNotFoundException: com.example.pricing.shared.MyClass,但我不明白为什么该类位于类路径上。
这是我正在使用的配置: 我在同一个 Tomcat 7 实例中有 2 个战争文件:
-
ROOT.war指向 www.example.com/ -
app.war指向 www.example.com/app/*
两个文件都包含 2 个共享库:
commons.jarpricing.jar
commons.jar 库包含扩展 Shiro 的 AbstractSessionDAO 的 SessionDAO 类。我的 SessionDAO 类是一个基本实现,它将 Shiro Session 保存在 MySQL 表中(VARCHAR id,BLOB Session 对象)。
现在,当我尝试访问 app.war 模块的 URL 时,在从数据库中反序列化 Session 时出现以下异常:
org.apache.shiro.session.SessionException: Class not found exception while reading Session from input stream!
com.example.dao.SessionDAO.deserialize(SessionDAO.java:97)
... (omitted)
Root cause:
java.lang.ClassNotFoundException: com.example.pricing.shared.MyClass
java.net.URLClassLoader$1.run(URLClassLoader.java:366)
错误的反序列化方法如下:
private static Session deserialize(ResultSet resultSet, String col){
ByteArrayInputStream bais=null;
ObjectInputStream ins=null;
Session res=null;
try{
byte[] barray=resultSet.getBytes(col);
if(barray!=null){
bais=new ByteArrayInputStream(barray);
ins=new ObjectInputStream(bais);
res=(Session)ins.readObject();
}
}catch(IOException e){
throw new SessionException("IO exception while reading Session from input stream!",e);
}catch(ClassNotFoundException e){
throw new SessionException("Class not found exception while reading Session from input stream!",e);//the one causing the issue
}catch(SQLException e){
throw new SessionException("SQL exception while reading Session from input stream!",e);
}finally{
try{
ins.close();
}catch(NullPointerException|IOException e){}
try{
bais.close();
}catch(NullPointerException|IOException e){}
}
return res;
}
你知道如何解决这个问题吗?
【问题讨论】:
标签: java deserialization classnotfoundexception shiro