发送未知类的Java对象 [英] Sending Java object of an unknown class
问题描述
搜索了很长时间,但没有找到答案。
searching for a long time, but didn't find any answer on that one:
我有一个服务器,有一个客户端。服务器应该通过 ObjectInputStream
和 ObjectOutputStream
接收对象。这对于服务器上已知的任何 class
已经有效。
现在,我想发送服务器不知道的类
的 Object
。他只知道该类的接口。那显然失败了……
I have a server and I have a client. The server should receive an object via ObjectInputStream
and ObjectOutputStream
. That already works for any class
, known on the server.
Now I want to send an Object
of a class
the server doesn't know. He only knows the interface of that class. And that obviously fails...
如何避免 ClassNotFoundException
?我以为接口就是解决方案。我只想访问通过接口知道的功能,但是Java还是想拥有该类。
How can I avoid the ClassNotFoundException
? I thought interfaces were the solution. I only want to access the functions I know by the interface, but Java wants to have the class anyway.
谢谢。
推荐答案
没有办法。为了能够在对象上执行方法,您需要具有对象具体类的字节码。其界面还不够。
There is no way. To be able to execute a method on an object, you need to have the byte-code of the concrete class of the object. Its interface isn't sufficient.
这篇关于发送未知类的Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!