1

I'm trying to convert java serialized files to python pkl files removing class definitions, just storing the raw data

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

I'm able to create an AdvancedFilterUserBean instance, but java.io.ObjectInputStream(fd).readObject() is unable to create the object

Has anybody got any ideas on how to make readObject() aware of my java class I imported

Thanks

Andy

  • Does your bean contains reference to other classes , do you have all the class definitions in the classpath ? – AllTooSir Jun 19 '13 at 11:27
  • ` package com.cds.magellon.reports.beans.common; import java.io.Serializable; public class AdvancedFilterUserBean implements Serializable{ private String filterName; private int propNameId; private int equality; private String filterValue; private boolean userDefined = false; private String propType = null; ... just getters and setters } ` – user2500822 Jun 19 '13 at 11:39
  • sorry about the non formatting above CLASSPATH is set to '.' I have com.cdn... dirs with my bean in the correct place I can create the bean from the python I start the app with jython test.py – user2500822 Jun 19 '13 at 11:48

0 Answers0