Rmi connection refused with localhost

前端 未结 5 1290
离开以前
离开以前 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:44

    It seems to work when I replace the

    Runtime.getRuntime().exec("rmiregistry 2020");
    

    by

    LocateRegistry.createRegistry(2020);
    

    anyone an idea why? What's the difference?

    0 讨论(0)
  • 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();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-06 02:01

    it seems that you should set your command as an String[],for example:

    String[] command = new String[]{"rmiregistry","2020"};
    Runtime.getRuntime().exec(command);
    

    it just like the style of main(String[] args).

    0 讨论(0)
  • 2020-12-06 02:08

    had a simliar problem with that connection exception. it is thrown either when the registry is not started yet (like in your case) or when the registry is already unexported (like in my case).

    but a short comment to the difference between the 2 ways to start the registry:

    Runtime.getRuntime().exec("rmiregistry 2020");
    

    runs the rmiregistry.exe in javas bin-directory in a new process and continues parallel with your java code.

    LocateRegistry.createRegistry(2020);
    

    the rmi method call starts the registry, returns the reference to that registry remote object and then continues with the next statement.

    in your case the registry is not started in time when you try to bind your object

    0 讨论(0)
  • 2020-12-06 02:08

    You need to have a rmiregistry running before attempting to connect (register) a RMI service with it.

    The LocateRegistry.createRegistry(2020) method call creates and exports a registry on the specified port number.

    See the documentation for LocateRegistry

    0 讨论(0)
提交回复
热议问题