Donate Share

JPype

Tracker: Bugs

5 ClassNotFoundException calling ObjectInputStream.readObject - ID: 1799807
Last Update: Comment added ( mgnozzio )

Using the attached SerialTest class, a ClassNotFoundException is raised
when ObjectInputStream.readObject is called directly to deserialize a
custom class instance. However, the method works fine when called from
Java code.

There is no problem deserializing a standard class instance, e.g. a
java.util.HashMap instance.

Example run:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from jpype import *
>>> startJVM(getDefaultJVMPath(), "-Djava.class.path=.")
>>> SerialTest = JClass("SerialTest")
>>> SerialTest(5,9).testSerial()
>>> bytes = SerialTest(6,10).serializeMe()
>>> test = SerialTest().deserializeMe(bytes)
>>> test.numOne
6
>>> test.numTwo
10
>>> bais = java.io.ByteArrayInputStream(bytes)
>>> ois = java.io.ObjectInputStream(bais)
>>> test2 = ois.readObject()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
jpype._jexception.ExceptionPyRaisable: java.lang.ClassNotFoundException:
SerialTest


Nobody/Anonymous ( nobody ) - 2007-09-21 18:40

5

Open

None

Steve Menard

jpype-win32

Version 0.5

Public


Comment ( 1 )




Date: 2007-10-15 19:36
Sender: mgnozzio


I also ran into this problem, so I did a little digging and found that it
was related to current class limitation documented in the the JPype user
manual. I don't know what needs to be done to fix it, but in the meantime,
an easy workaround is to write a Java class that extends the
ObjectInputStream class and overrides the resolveClass method, so that it
uses the System class loader. Then, you can use this new class wherever
you would use the ObjectInputStream class and things load without any
problem. So, more specifically, I wrote the following class and
substituted it for the built-in ObjectInputStream class:

package com.adverplex.captcha;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.IOException;

public class JPypeObjectInputStream extends ObjectInputStream {

public JPypeObjectInputStream(InputStream in) throws IOException
{
super(in);
}

protected Class<?> resolveClass(ObjectStreamClass desc) throws
IOException, ClassNotFoundException
{
return Class.forName(desc.getName(), true,
ClassLoader.getSystemClassLoader());
}
}




Log in to comment.

Attached File ( 1 )

Filename Description Download
SerialTest.java SerialTest class Download

Change ( 1 )

Field Old Value Date By
File Added 246642: SerialTest.java 2007-09-21 18:40 nobody