Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

RMI and object serialization

Saving and retrieving objects with Java

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Recently JavaSoft has made available an alpha release of the serialization and RMI classes. This new API is extremely useful and important to master. It allows developers to save and retrieve Java classes to any I/O stream. Objects can be saved and retrieved at later points in time. The supplied classes do not provide any namespace management for the saved objects. This article will cover:

  • saving and retrieve objects to a local disk.
  • saving objects over a network to a server on a known port.
  • some performance metrics for you to think about.
  • learn how to write Java programs that are both applications and applets -- where an application means it contains a main().


We'll conclude with some more discussion of the specifics of the implementation. This column teaches concepts and the applets don't really do anything useful other than serve as an instructional vehicle.

The following examples demonstrate the use of the serialization technology that has been included in JDK 1.0.2. An add-on package is required to run the following applets. The applets below were run with the JDK appletviewer on Windows 95 and Solaris. I was unable to find a browser manufacturer that clearly stated what version of the JDK they were running and decided that it was best to wait for Microsoft and Netscape to get up to speed with the up and coming JDK 1.1 release. If you have problems installing the serialization classes needed on your system try looking at this FAQ. The javaSoft team has also been nice enough to collect all the mail messages for those of you wishing to find quick answers to questions.

Setting up the environment on your local machine

  1. Get and install the RMI classes following the directions provided with them. Make sure you set your CLASSPATH environment variable to include the lib/rmi.zip file in the RMI directory.
  2. Download and unzip the example files provided. Install these files into a directory and add it to CLASSPATH. Use the appletviewer or the Java virtual machine to run the applets below.


Reading and writing Java objects to a local disk

The following two applets save an object and retrieve some classes. Because they are writing to the disk you will have to run these programs locally on your system. The ShowSavingAClass applet saves a String and a Date class to a disk file called SavedObject in the current directory. ShowReadingAClass reads the saved file SavedObject and recreates the classes contained in it. If your browser has RMI support then you can run the following two applets:



Writing Java objects to a server

The programs require that a server program be running on the server so that an applet or Java application can communicate over the port to save objects.

The program WriteObjectsFromSocket reads an object from a client and saves it on the server in a fixed area. Here's the source. To create a file on a server that contains an instance of the object run the following:

  • java WriteObjectsFromSocket servername portnumber,
  • The corresponding client piece is ShowSavingAClassNet. Here's the source. Don't forget to modify the HTML parameter tags if you host this applet on your local system.


The source code for ShowSavingAClassNet.java contains an example of designing applets that use the same source code for both so that users can start them with or without the applet viewer.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources