类反序列化 – Jython和Java之间的差异

huangapple go评论71阅读模式
英文:

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. &lt;br&gt;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(&quot;Nome&quot;), [self._insertN])

    firstBlock = (nameLabelPane, nameFieldPane)

    firstPane = gridPane(firstBlock)

    if self._title.startswith(&#39;Modifica&#39;):
        saveBtn = JButton(&#39;Modifica&#39;)
    else:
        saveBtn = JButton(&#39;Salva&#39;)

    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, &#39;Frame di prova&#39;)
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 = &quot;localhost&quot;
mysqlLog_username = &quot;root&quot;
mysqlLog_password = &quot;&quot;
mysqlLog_database = &quot;tuttle&quot;

properties = Properties()
properties.put(&quot;user&quot;, mysqlLog_username)
properties.put(&quot;password&quot;, mysqlLog_password)

url = &quot;jdbc:mysql://&quot; + mysqlLog_server + &quot;/&quot; + mysqlLog_database + \
    &quot;?user=&quot; + mysqlLog_username + &quot;&amp;password=&quot; + 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&#39;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. &lt;br&gt;
It almost seems like the deserializing function doesn&#39;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>



huangapple
  • 本文由 发表于 2020年9月4日 20:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定