Rmi connection refused with localhost

前端 未结 5 1300
离开以前
离开以前 2020-12-06 01:27

I have a problem using java rmi:

When I\'m trying to run my server, I get a connectException (see below).

Exception happens when executing the rebind method:

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 01:55

    One difference we can note in Windows is:

    If you use Runtime.getRuntime().exec("rmiregistry 1024");

    you can see rmiregistry.exe process will run in your Task Manager

    whereas if you use Registry registry = LocateRegistry.createRegistry(1024);

    you can not see the process running in Task Manager,

    I think Java handles it in a different way.

    and this is my server.policy file

    Before running the the application, make sure that you killed all your existing javaw.exe and rmiregistry.exe corresponds to your rmi programs which are already running.

    The following code works for me by using Registry.LocateRegistry() or

    Runtime.getRuntime.exec("");
    
    
    // Standard extensions get all permissions by default
    
    grant {
        permission java.security.AllPermission;
    };
    

    VM argument

    -Djava.rmi.server.codebase=file:\C:\Users\Durai\workspace\RMI2\src\
    

    Code:

    package server;    
    
    import java.rmi.Naming;
    import java.rmi.RMISecurityManager;
    import java.rmi.Remote;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    
    public class HelloServer 
    {
      public static void main (String[] argv) 
      {
        try {
    
            if(System.getSecurityManager()==null){
                System.setProperty("java.security.policy","C:\\Users\\Durai\\workspace\\RMI\\src\\server\\server.policy");
                System.setSecurityManager(new RMISecurityManager());
            }
    
     Runtime.getRuntime().exec("rmiregistry 1024");
    
     //     Registry registry = LocateRegistry.createRegistry(1024);
       //   registry.rebind ("Hello", new Hello ("Hello,From Roseindia.net pvt ltd!"));
       //Process process = Runtime.getRuntime().exec("C:\\Users\\Durai\\workspace\\RMI\\src\\server\\rmi_registry_start.bat");
    
            Naming.rebind ("//localhost:1024/Hello",new Hello ("Hello,From Roseindia.net pvt ltd!")); 
          System.out.println ("Server is connected and ready for operation.");
        } 
        catch (Exception e) {
          System.out.println ("Server not connected: " + e);
          e.printStackTrace();
        }
      }
    }
    

提交回复
热议问题