0

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.

Vishnu Kumar
  • 59
  • 1
  • 2
  • 7
  • You can see the error in the cmd prompt by clicking on the "enter image description" in the question above – Vishnu Kumar Oct 29 '13 at 03:13
  • What new exception? What problem? Post the exception, and the code, *here.* Most people won't bother to follow links, and the question has no permanent value unless it contains all the relevant material. – user207421 Oct 29 '13 at 03:45
  • http://stackoverflow.com/questions/2427473/java-rmi-accesscontrolexception-access-denied – Kevin Panko Oct 29 '13 at 04:16
  • 1 - assign the policy file correctly, either with a commandline flag: java -Djava.security.policy=/home/.../.policy .... .My all java files are in C:/java/PhoneBook folder. How to ssign policy file for my java files? – Vishnu Kumar Oct 29 '13 at 16:33
  • 2 - make sure the format of the policy file is correct, e.g.: 'grant codeBase "file:/bin/-" { permission java.security.AllPermission; };'.Where i have to type the above code? – Vishnu Kumar Oct 29 '13 at 16:36
  • could you help me how to assign policy file for my project. all files are in c:/java/PhoneBook folder and also the how to get permission. @KevinPanko – Vishnu Kumar Oct 29 '13 at 17:55
  • Don't use a security manager, don't use a policy file, don't specify a policy file, do not pass GO, do not collect $200. – user207421 Oct 29 '13 at 22:00

1 Answers1

0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • i removed one Security manager in both client and server. now i get an exception when i started the server: java PhoneBookServer ' java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)' – Vishnu Kumar Oct 29 '13 at 15:31
  • Yon can only get that exception if there is a security manager installed. I did specifically say to remove *both* in the server. – user207421 Oct 29 '13 at 21:58
  • first i tried without any security manager,then the server showed the error access denied, then i installed the security manager so that it might connect, but still it shows the same exception – Vishnu Kumar Oct 30 '13 at 02:37
  • No you didn't. It is impossible for the server to show 'access denied' without a security manager. Look at your own stack trace. The `AccessException` is thrown via the `SecurityManager.` – user207421 Oct 30 '13 at 09:07
  • i get the following exception when i started the server without any security manager 'java.rmi.connectException: connection refused to host 127.0.0.1;nested exception is java.net.connectException:connection timed out'. – Vishnu Kumar Oct 30 '13 at 14:08
  • i could have updated the exception image but the edit option is not working – Vishnu Kumar Oct 30 '13 at 14:09
  • I tried with and without security manager, Either way the server is not starting, could anybody please help to solve the problem. Is there any body who could provide the solution???? – Vishnu Kumar Oct 31 '13 at 01:03
  • You have a network problem in your host. Nothing to do with RMI. Can you ping 127.0.0.1? Investigate that. – user207421 Oct 31 '13 at 01:05