javapermissionsrmiaccesscontrolexception

how a client connect to server in Java RMI?


enter image description hereI am developing a simple phone book application in in RMI. I am able to start RMI registry and stub class is also generated for the Implementation c1ass. Now i have started the server by using command Java PhoneBookServer in my cmd prompt. The next step is to start the client,So after the i started the client the following error arises! Both the client and servers programs are in a single folder enter image description here

The code i used so far for client and server are as follows

     import java.rmi.*;
     import java.rmi.server.*;

     public class PhoneBookServer {
     public static void main  (String[] args){

    /*Create and install a security manager

      SecurityManager appsm = System.getSecurityManager();
          if(appsm==null){

          System.setSecurityManager(new RMISecurityManager()); 

       }*/

       System.out.println("Server is started");

     try
      {
        //create PhoneBookImpl
        PhoneBookImpl Pb=new PhoneBookImpl();
        Naming.rebind("rmi://127.0.0.1:1099/PhoneBook", Pb);
      }
        catch(Exception e)
       {
          System.out.println("Exception is:" +e);
     }

     }

    }

Client Program

     import java.rmi.*;
     import java.rmi.registry.*;
     import java.rmi.server.*;
     import java.lang.*;
     import java.io.*;
     import java.util.Scanner;

     public class PhoneBookClient {
     public static void main  (String[] args) throws Exception
     {

      /*Create and install a security manager

          SecurityManager appsm = System.getSecurityManager();
          if(appsm==null){
                  System.setSecurityManager(new RMISecurityManager()); 

       }*/

      String name,number,total,choice,id;
      String ch;
      PhoneBook pb=(PhoneBook)Naming.lookup("rmi://127.0.0.1:1099"+"/PhoneBook");
      Scanner in=new Scanner(System.in);
      System.out.println("1.Enter new record /n");
      System.out.println("2.Look up record /n");
      System.out.println("3.Delete record /n");
      System.out.println("Enter your option");
      ch=in.nextLine();

      if(ch.equals("1")){
      do{
         System.out.println("Enter unique id:");
         id=in.nextLine();
         System.out.println("Enter name:");
         name=in.nextLine();
         System.out.println("Enter phone number:");
         number=in.nextLine();
         total=name+"  "+number;
         pb.new_record(id,total);
         System.out.println("Enter 'q' to quit or enter 'p' to proceed");
         choice=in.nextLine();
         }while(!choice.equals("q"));

       }
      if(ch.equals("2")){
         do{
          System.out.println("Enter id to look up a record"+"  enter 'q' to quit");
          id=in.nextLine();
          String record=pb.lookup_record(id);
          System.out.println("The record is" +record);
          }while(!id.equals("q"));
                      }

     if(ch.equals("2")){
        do{
          System.out.println("Enter id to delete a record"+"  enter 'q' to quit");
          id=in.nextLine();
          pb.lookup_record(id);
          System.out.println("The record is deleted");
        }while(!id.equals("q"));

                   }
         }
      }

Previously i got the exception:

Connection refused to the host127.0.0.1 access denied.

So I install the security mnager in my client and server programs. Now I get this new type of exception. How can I solve the problem.


Solution

  • Get rid of the security manager. You don't need it in this configuration. Get rid of both security manager installs in the server: you can only install one; and get rid of it in the client too.

    I don't see how that server code can possibly work at all with the two security managers. That can't be the real code.