Sie sind auf Seite 1von 2

'Hello World' example of an Applet using Java RMI

https://eyeasme.com/Shayne/RMI/index.html

'Hello World' example of an Applet using Java RMI


'Hello World' example of an Applet using Java RMI (Remote Method Invocation), with source code and notes included.

Source Code
The files for this example should all be placed in the same directory. RemoteInterface.java 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; }

Server.java 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: 1. Get a reference to the registry located at some port (default port is 1099) [here we are using RemoteInterface.REGISTRY_PORT ]. 2. Create a remote reference of the object (do this by exporting the object) [use UnicastRemoteObject.exportObject method]. 3. In the registry bind the remote reference to some name [here we are using 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); } } }

ClientApplet.java NOTES: The steps to getting and using a remote object are: 1. Get a reference to the registry at some host on some port [here using getCobeBase().getHost() to get the host and RemoteInterface.REGISTRY_PORT for the port]. 2. Retrieve a remote reference by looking it up by its name in the registry [here using RemoteInterface.REGISTRY_NAME]. 3. Use the remote reference to call its methods [all the methods it can call are declared in 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 import import import java.rmi.registry.LocateRegistry; java.rmi.registry.Registry; javax.swing.JApplet; 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); } } }

applet.html 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"

1 of 2

3/6/2012 10:13 AM

'Hello World' example of an Applet using Java RMI

https://eyeasme.com/Shayne/RMI/index.html

"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>

applet2.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>

Compiling and Running


These commands to be executed from the directory where the above files are: 1. Compile the code (i.e., Server.java, ClientApplet.java, and RemoteInterface.java). This will generate Server.class, ClientApplet.class, and RemoteInterface.class
javac *.java

2. Compile the server with the RMI compiler. For Java versions before 1.5
rmic -v1.2 Server

For Java versions 1.5 and later


rmic Server

this will generate the file: Server_Stub.class 3. Start the RMI registry service and run it in the background. (Usage) rmiregistry [port] & By default, the RMI registry listens on port 1099. There is no need to restart the RMI registry if it is already running. A Java program to see if a RMI registry is running and what names are registered in it. (Unix) rmiregistry 3273 & (Windows) start rmiregistry 3273 4. Start the server and run it in the background. (Unix) java Server & (Windows) start java Server 5. Open a web-browser and view applet.html

If the applet does NOT work, here are some possible reasons why
Make sure the .class and .html files are readable.
The following is a Unix command that makes all files in this directory readable: chmod 644 *

You might be using a very old Java Plug-in; get the latest Java Plug-in here. Make sure the RMI registry service and the Server application processes are running.
The following is a Unix command that will list a user's processes: ps cux

Make sure that there are no firewall issues. The Client must be able to open a connection to the RMI registry port on the Server.
Copyright 2011 Shayne Steele (shayne.steele <AT> eyeasme <DOT> com This document is licensed under the GFDLv1.3, All software shown is licensed under the LGPLv3. Document last modified on August 06, 2011 02:26 pm MST.

2 of 2

3/6/2012 10:13 AM

Das könnte Ihnen auch gefallen