1

我正在尝试将 java 序列化文件转换为删除类定义的 python pkl 文件,只存储原始数据

import java.io
import cPickle as pickle
from pprint import pprint
import os
import com.cds.magellon.reports.beans.common

def toPython(val):
  if hasattr(val,'getClass'):
    className = val.getClass().getName()
    if className == 'java.util.Hashtable':
       return toDict(val)
    if className == 'java.util.Vector':
      vals = []
      for x in val:
        vals.append(toPython(x))
    else:
      print val.getClass().getName()
      return None
  else:
    return val

def toDict(jobj):
  obj = {}
  for k in jobj.keys():
    val = jobj[k]
    obj[k] = toPython(val)
  return obj

def convert(dir):
  for filename in os.listdir(dir):
    if filename.endswith('.pkl'):
      continue
    fd = file(os.path.join(dir,filename),'rb')
    try:
      obj = java.io.ObjectInputStream(fd).readObject()
      pobj = toPython(obj)
      pickle.dump(pobj,file(os.path.join(dir,filename + ".pkl"),'wb'))
      print "Saved", filename
    except Exception, e:
      print "Failed", filename, e

a = com.cds.magellon.reports.beans.common.AdvancedFilterUserBean()
a.setFilterName('FRED')
print a.getClass(),a.getFilterName()
convert('perfmonitors')

[Andy@bagend www]$ jython test.py
com.cds.magellon.reports.beans.common.AdvancedFilterUserBean FRED

Traceback (innermost last):
  File "test.py", line 54, in ?
  File "test.py", line 38, in convert
   at java.lang.Class.forName(libgcj.so.7rh)
   at java.io.ObjectInputStream.resolveClass(libgcj.so.7rh)
   at java.io.ObjectInputStream.readClassDescriptor(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.io.ObjectInputStream.readArrayElements(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.io.ObjectInputStream.readFields(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.util.Hashtable.readObject(libgcj.so.7rh)
   at java.lang.reflect.Method.invoke(libgcj.so.7rh)
   at java.io.ObjectInputStream.callReadMethod(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.util.Hashtable.readObject(libgcj.so.7rh)
   at java.lang.reflect.Method.invoke(libgcj.so.7rh)
   at java.io.ObjectInputStream.callReadMethod(libgcj.so.7rh)
   at java.io.ObjectInputStream.readObject(libgcj.so.7rh)
   at java.lang.reflect.Method.invoke(libgcj.so.7rh)

java.lang.ClassNotFoundException: java.lang.ClassNotFoundException:       
com.cds.magellon.reports.beans.common.AdvancedFilterUserBean

我能够创建 AdvancedFilterUserBean 实例,但 java.io.ObjectInputStream(fd).readObject() 无法创建对象

有没有人知道如何让 readObject() 知道我导入的 java 类

谢谢

安迪

4

0 回答 0