Completing Client-side Files
The RMI module generates client-side files with main() methods that look
like the following example:
public static void main (String[] args) {
System.setSecurityManager (new RMISecurityManager());
}
To complete your client class, you must add a lookup() call
that returns a reference to the correct RMI server. Then you can use the
reference to invoke server-side methods.
Adding the lookup() Call
-
The RMI server program should be running. You need to know which RMI registry
it is registered with. In a large development effort, the server may be
under control of a system administrator or another developer. If you are
doing all of the development work you can start the server yourself.
-
Start the RMI Module's Registry Browser, specifying the registry in which
the RMI server is registered.
-
Open the Registry Browser's RMI Registry node. Click through the
node hierarchy and locate the subnode that represents the RMI server's
remote interface.
-
Right-click on the remote interface node. This opens a context menu; select
Copy
Client Code.
-
Return to your client class code in the IDE's editing window.
-
Place the cursor underneath the line with the setSecurityManager()
call. Paste the code from the clipboard. After you paste, the main()
method should resemble the following example:
public static void main (String[] args) {
System.setSecurityManager (new RMISecurityManager());
try {
Hello obj = (Hello) Naming.lookup
("//localhost:1099/Remote");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Adding Server Method Invocations
-
The object returned by the lookup is a proxy for the RMI server. You can
invoke methods on it.
-
Use code like this:
obj.sayHello();
-
Your completed main method will look like this:
public static void main (String[] args) {
System.setSecurityManager (new RMISecurityManager());
try{
Hello obj = (Hello) Naming.lookup
("//localhost:1099/Remote");
obj.sayHello();
}
catch (Exception ex)
{
ex.printStackTrace();
}
Compiling and Running Your Client
-
The RMI server must be running before you can successfully run your client.
-
In the Explorer Window's Filesystems tab, right-click on your client class
and open its property sheet. Make sure that the Executor property
is set to RMI Client Execution.
-
Right-click on the node for the package that contains your client and choose
Compile
from the context menu.
-
Right-click again on the node for the client class and choose Execute
from the context menu.
For more information see:
Legal Notices