'Hello World' example of an Applet using Java RMI (Remote Method Invocation), with source code and notes included.
The files for this example should all be placed in the same directory.
NOTES:
All methods that the Server wants to make available to the
Client [here called ClientApplet
] MUST declare that they can
throw a RemoteException
.
For this example, rather than use the RMI registry default port of 1099, a
different port number will be used (to show how it is done).
import java.rmi.Remote; import java.rmi.RemoteException; public interface RemoteInterface extends Remote { String REGISTRY_NAME = "RMI_Example"; int REGISTRY_PORT = 3273; String getMessage() throws RemoteException; }
NOTES:
A Server does NOT need to extend UnicastRemoteObject
.
The Server's method(s) [here only one named getMessage
] being made
available via the extension to the
Remote
interface [here named
RemoteInterface
]
do NOT need to throw
RemoteException
, but DO
need to say that they CAN throw RemoteException
in
the definition of the extended interface [here
RemoteInterface
].
The steps to exporting an object are:
RemoteInterface.REGISTRY_PORT
].
UnicastRemoteObject.exportObject
method].
RemoteInterface.REGISTRY_NAME
].
For this example, rather than deal with all the checked exceptions, all exceptions are turned into runtime exceptions so that the runtime system can handle them (i.e stop running and print the exceptions to the console).
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class Server implements RemoteInterface { public String getMessage() { return "Hello World"; } public static void main(String args[]) { try { Registry registry = LocateRegistry.getRegistry(RemoteInterface.REGISTRY_PORT); RemoteInterface remoteReference = (RemoteInterface) UnicastRemoteObject.exportObject(new Server()); registry.rebind(RemoteInterface.REGISTRY_NAME, remoteReference); } catch (Exception e) { throw new RuntimeException(e); } } }
NOTES:
The steps to getting and using a remote object are:
getCobeBase().getHost()
to get
the host and RemoteInterface.REGISTRY_PORT
for the port].
RemoteInterface.REGISTRY_NAME
].
RemoteInterface
and all can throw a checked exception
of type RemoteException
] the same way you
would use any regular reference to call its methods that can
throw a checked exception.
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.swing.JApplet; import javax.swing.JLabel; public class ClientApplet extends JApplet { public void init() { try { Registry registry = LocateRegistry.getRegistry(getCodeBase().getHost(), RemoteInterface.REGISTRY_PORT); RemoteInterface remoteReference = (RemoteInterface) registry.lookup(RemoteInterface.REGISTRY_NAME); getContentPane().add(new JLabel(remoteReference.getMessage())); } catch (Exception e) { throw new RuntimeException(e); } } }
NOTES:
This is the modern XHTML standard way of displaying an applet (as an
object
). For an explanation of this code see
here.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>'Hello World' via an Applet using Java RMI</title> </head> <body> <h1>'Hello World' via an Applet using Java RMI</h1> <p> If everything works correctly there should be an applet running below that says 'Hello World': </p> <p> <!--[if !IE]>--> <object classid="java:ClientApplet.class" type="application/x-java-applet" height="50" width="150" > <!--<![endif]--> <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" height="50" width="150" > <param name="code" value="ClientApplet" /> </object> <!--[if !IE]>--> </object> <!--<![endif]--> </p> </body> </html>
NOTES:
This is the less modern HTML standard way of displaying an applet.
Either .html
file (
applet.html or
applet2.html) will
display the applet on all modern browsers,
applet.html is the preferred file to
use, but applet2.html might work
better on some extremely old browsers.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>'Hello World' via an Applet using Java RMI</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h1>'Hello World' via an Applet using Java RMI</h1> <p> If everything works correctly there should be an applet running below that says 'Hello World': </p> <p> <!-- Compatible with all modern browsers --> <applet code="ClientApplet" height="50" width="150"></applet> </p> </body> </html>
These commands to be executed from the directory where the above files are:
javac *.java
rmic -v1.2 Server
rmic Server
rmiregistry [port] &
rmiregistry 3273 &
start rmiregistry 3273
java Server &
start java Server
chmod 644 *
ps cux