views:

74

answers:

3

I am trying to send a class over a TCP 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.

+1  A: 

You probably need to use a ClassLoader.

Gian
+3  A: 

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:


If you're actually sending a *.class file, using a ClassLoader to load it would indeed be the answer.

BalusC
+1  A: 

If possible for you, you may want to look at RMI where clients can provide classes for the server to invoke.

Thorbjørn Ravn Andersen