I am trying to send a class over a 开发者_如何学JAVATCP connection using java. When the client receives the class it should invoke the methods of that class and return the result to the server.
I have serialized the class to a byte array to send to the client, but I don't know with it once the client receives it.
Thank you.
Your question is a bit ambiguous. Are you sending a *.class
file or an instance of the class? I'll bet that you actually mean an instance of the class since you literally said that you want to send it back. This makes only sense if it were an instance. The other side should then have the class file in its classpath as well. Then you can just import
the class and cast to the desired class on readObject()
. Finally you'll be able to invoke methods on it according the class' contract.
See also:
- Basic Serialization tutorial
- Advanced Serialization tutorial
If you're actually sending a *.class
file, using a ClassLoader
to load it would indeed be the answer.
You probably need to use a ClassLoader.
If possible for you, you may want to look at RMI where clients can provide classes for the server to invoke.
精彩评论