views:

207

answers:

4

I define a class, and then I instate an object of that class type. I want to send this object to another Java application running on a different computer transparently. What is the best technology to accomplish this?

+7  A: 

You'll want to start by looking into serialization with the Java Serializable interface. Sun has a good article on it called Discover the secrets of the Java Serialization API.

Refer to the Java Sockets tutorial for information on actually transferring the serialized object over the network.

Bill the Lizard
+5  A: 

There are a lot of ways to do this. Here are some things to look into and you can pick the one that would work best for your application.

  • J2EE
  • RMI
  • Object Serialization pushing the bits over a Socket
  • Webservices

Pretty much any communication framework will allow you to push objects over a network in one way or another. You just need to review them and see which works for your application. A quick google should find even more methods.

Rodney Foley
+5  A: 

you can create object streams using the java api and send any serializable object. but youll have to mind that these go unencrypted through the network:

on the sender's side:

CustomObject objectToSend=new CustomObject();
Socket s = new Socket("yourhostname", 1234);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(objectToSend);
out.flush();

and on the receiving end:

ServerSocket server = new ServerSocket(1234);
Socket s = server.accept();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
CustomObject objectReceived = (CustomObject) in.readObject();
smeg4brains
+2  A: 

A (de facto) standard to implement this would be to use a web service, for example using JAX-WS which is bundled in Java 6. See this tutorial for a java-first sample (i.e. using annotations). This is pretty straight forward and easy.

There are other approaches such as Serialization over a Socket, RMI, EJBs but, when working over the Internet, web services are a kind of natural choice as they rely on existing standards (SOAP, HTTP) and deal easily with firewalls (which might be a real issue for all other solutions).

Pascal Thivent