英文:
Class deserialization - Differences between Jython and Java
问题
我能够将自定义的JFrame序列化到数据库中,现在我想要对其进行反序列化。可惜的是,在尝试进行反序列化时,我遇到了一个无法解决的错误。
错误发生在这行代码上:`objFromDatabase = deSerializeJavaObjectFromDB(connection)`,具体地说是在`deSerializedObject = objectIn.readObject()`这一行上,错误信息如下:
```python
Traceback (most recent call last):
File "provaSerializzazione.py", line 119, in <module>
main(sys.argv)
File "provaSerializzazione.py", line 113, in main
objFromDatabase = deSerializeJavaObjectFromDB(connection)
File "provaSerializzazione.py", line 74, in deSerializeJavaObjectFromDB
deSerializedObject = objectIn.readObject()
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.python.proxies.__main__$MyFrame$3
有趣的是,如果你将frame = MyFrame()
这一行代码替换为frame = JFrame()
,函数会正确地进行序列化,没有错误产生。看起来仿佛是反序列化函数无法接受一个Python类(这个类又继承自JFrame)。
请问在Jython语言中,我应该如何做才能对MyFrame
类进行反序列化?先提前谢谢你的帮助!
<details>
<summary>英文:</summary>
I was able to serialize a custom JFrame into a database, and I would like to deserialize it. <br>Unfortunately, during the alleged deserialization I have an error that I cannot resolve.
import sys
from java.io import *
from java.lang import Class
from java.sql import *
from methods import *
class MyFrame(JFrame):
def init(self):
self._title = 'Frame di prova'
self.setTitle(self._title)
c = self.getContentPane()
self._insertN = JTextField(20)
nameLabelPane, nameFieldPane = incidentPanels(
JLabel("Nome"), [self._insertN])
firstBlock = (nameLabelPane, nameFieldPane)
firstPane = gridPane(firstBlock)
if self._title.startswith('Modifica'):
saveBtn = JButton('Modifica')
else:
saveBtn = JButton('Salva')
globalPane = JPanel()
gridBagger(globalPane, (firstPane, saveBtn))
c.add(JScrollPane(globalPane))
self.pack()
SQL_CREATE_TABLE = "create table if not exists serialized_java_objects (object_name varchar(1000), serialized_object blob)"
SQL_SERIALIZE_OBJECT = "INSERT INTO serialized_java_objects(object_name, serialized_object) VALUES (?, ?)"
SQL_DESERIALIZE_OBJECT = "SELECT serialized_object FROM serialized_java_objects limit 1"
def createTable(connection):
connection.createStatement().executeUpdate(SQL_CREATE_TABLE)
def serializeJavaObjectToDB(connection, objectToSerialize):
pstmt = connection.prepareStatement(SQL_SERIALIZE_OBJECT)
pstmt.setString(1, 'Frame di prova')
pstmt.setObject(2, objectToSerialize)
pstmt.executeUpdate()
pstmt.close()
def deSerializeJavaObjectFromDB(connection):
pstmt = connection.prepareStatement(SQL_DESERIALIZE_OBJECT)
rs = pstmt.executeQuery()
rs.next()
buf = rs.getBytes(1)
objectIn = None
if (buf != None):
objectIn = ObjectInputStream(ByteArrayInputStream(buf))
deSerializedObject = objectIn.readObject()
rs.close()
pstmt.close()
return deSerializedObject
def getMySqlConnection():
mysqlConn = None
mysqlLog_server = "localhost"
mysqlLog_username = "root"
mysqlLog_password = ""
mysqlLog_database = "tuttle"
properties = Properties()
properties.put("user", mysqlLog_username)
properties.put("password", mysqlLog_password)
url = "jdbc:mysql://" + mysqlLog_server + "/" + mysqlLog_database + \
"?user=" + mysqlLog_username + "&password=" + mysqlLog_password
mysqlConn = DriverManager.getConnection(url)
return mysqlConn
def main(args):
frame = MyFrame()
Class.forName("com.mysql.jdbc.Driver")
connection = getMySqlConnection()
createTable(connection)
serializeJavaObjectToDB(connection, frame)
objFromDatabase = deSerializeJavaObjectFromDB(connection)
objFromDatabase.setVisible(True)
if name == 'main':
main(sys.argv)
The error is at the line `objFromDatabase = deSerializeJavaObjectFromDB(connection)`, In particular on `deSerializedObject = objectIn.readObject()` and it's the following:
Traceback (most recent call last):
File "provaSerializzazione.py", line 119, in <module>
main(sys.argv)
File "provaSerializzazione.py", line 113, in main
objFromDatabase = deSerializeJavaObjectFromDB(connection)
File "provaSerializzazione.py", line 74, in deSerializeJavaObjectFromDB
deSerializedObject = objectIn.readObject()
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.python.proxies.main$MyFrame$3
Curious the fact that, if you replace the line `frame = MyFrame()` with `frame = JFrame()` the function serializes correctly, without errors. <br>
It almost seems like the deserializing function doesn't accept a python class (which in turn extends a JFrame).
What should I do to deserialize my `MyFrame` class in the jython language? Thanks in advance!
</details>
# 答案1
**得分**: 1
你需要使用 `org.python.util.PythonObjectInputStream` 替代 `java.io.ObjectInputStream`。
<details>
<summary>英文:</summary>
You need to use `org.python.util.PythonObjectInputStream` instead of `java.io.ObjectInputStream`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论