import java.rmi.*; import java.net.*; public class HelloClient { public static void main(String args[]) { // Get the url of the HelloServer String urlServerStr = ""; if( (args.length > 1) || (args.length == 1 && !args[0].startsWith("rmi:")) ) { System.err.println( "Usage: java HelloClient [rmi://host.domain:port/helloServer]"); System.exit(1); } else if ( args.length == 0 ) { urlServerStr = "rmi://localhost/helloServer"; } else { urlServerStr = args[0]; } // Starts Security Manager to download stub_class and execute it ! if( System.getSecurityManager() == null ) { System.setSecurityManager(new RMISecurityManager()); } try { Hello h = (Hello)Naming.lookup(urlServerStr); String message = h.sayHello(); System.out.println("\n HelloClient: msg from server -> " + message); } catch (MalformedURLException e) { System.out.println(urlServerStr + " is not a valid RMI URL" + e.getMessage()); e.printStackTrace(); } catch (RemoteException e) { System.out.println("Remote object threw exception " + e.getMessage()); e.printStackTrace(); } catch (NotBoundException e) { System.out.println("Could not find the requested remote object: " + e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.out.println("Exception in main: " + e); e.printStackTrace(); } } }