!--a11y-->
Implementing a Client 
When implementing the client, you must write code that does the following:
...
1. Obtains a reference to the remote object by getting an initial context from the Naming System.
2. Looks up the implementation of the remote object.
3. Calls remote methods of that object.
The BankClient class is an example of a client that looks up the bank remote object from the naming system and then calls methods on it remotely.

package exapmles.rmi_p4;
import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties;
public class BankClient {
public static void main(String[] args) { try { if (args.length < 2) { System.out.println(" Use: BankClient <hostName> <port>"); return; } Properties p = new Properties(); // Specify the type of the initial context factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl"); // Specify the provider URL. p.put(Context.PROVIDER_URL, args[0] + ":" + args[1]); // Specify the security principal (user). p.put(Context.SECURITY_PRINCIPAL, args[2]); // Specify the security credentials (password). p.put(Context.SECURITY_CREDENTIALS, args[3]);
// Connect to the server by the initial context. Context initialContext = new InitialContext(p); Account account = (Account) initialContext.lookup("Bank"); // Invoke methods remotely. account.deposit(100); System.out.println("Balance:" + account.getBalance()); System.out.println("Try to draw..."); account.draw(50); System.out.println("Balance:" + account.getBalance()); } catch (Exception ex) { ex.printStackTrace(); } } } |
