Chapter 3

The Classic Hello World Program



Universal TeacherIn this chapter, we will create a distributed version of the classic Hello World program using Remote Method Invocation (RMI). In creating this application, we first design a Remote interface that defines the remote methods, then write a server application that implements the remote interface, and finally write a client that uses the interface.

The source code needed for this example is stored in the HOME\examples\HelloWorld directory. The HelloWorld directory contains the following files:

  • Compile.bat (to compile all the files)
  • Rmicompile.bat (to generate Stub and Skeleton )
  • RunServer.bat (to run the server application)
  • RunClient.bat ( to start the client application)



HOME is the directory in which this ebook is installed.


Construction Zone

This is where you get your hands dirty.

Creating Remote Interface

A remote object must implement atleast one remote interface. A single class may implement more than one remote interface also. The implementing class may have methods that are not declared in the remote interface, but only the methods that are declared in the remote interface would be available to the client. Remote interfaces have the following characteristics:

  • The remote interface must be declared public. Otherwise, a client will get an error message.
  • Each method declaration in a remote interface must include java.rmi.RemoteException or one of its superclasses in its throws clause.
  • The remote interface must directly or indirectly extend the java.rmi.Remote interface. A remote interface may extend any other non-remote interface, but the methods declared in that non-remote interface must satisfy the requirements of remote method declaration.

Listing 3.1

import java.rmi.*;

//Remote Interface
public interface HelloInterface 
	extends Remote {

//remote method. Returns a string object
public String getMessage()
	throws RemoteException;

Explanation of Code

Here, we have defined a public interface, HelloInterface, which extends java.rmi.Remote interface. The interface contains only one method that returns a string. The getMessage() method throws a RemoteException because a remote method invocation can fail due to the following reasons:

  • Server related problems.
  • Protocol errors.
  • Failure during marshalling or unmarshalling.

A remote method throws a RemoteException, if communication fails. Any client that calls this method must use a try/catch block or declare that it throws a RemoteException.




RMI BOOK MAIN PAGE                Top

Copyright © 2001