title: RMI(java.net.ConnectException: Connection refused: connect)
body: <p>I am trying to run my server named SampleServer. I am using windows and this is what i did:</p>
  
  <p>in cmd:</p>
  
  <pre><code>javaw rmiregistry 1099
  cd C:\Users\Home\workspace\RMI\src
  java -Djava.security.policy=policy SampleServer 1099
  </code></pre>
  
  <p>i get the following error:</p>
  
  <pre><code>binding //localhost:1099/Sample
  New instance of Sample created
  Sample server failed:Connection refused to host: localhost; nested exception is:
  
          java.net.ConnectException: Connection refused: connect
  </code></pre>
  
  <p>I've tried using a different port # such as 4719 for rmiregistry but i receive the same error. I made sure that my firewall was disabled but the problem persist. I made sure that the port is not already being used. I really hope someone can help me.</p>
  
  <p>Picture of my desktop with folders of project, eclipse window and cmd open:
  <a href="http://s22.postimg.org/uq00qzslr/picyture.png" rel="nofollow">http://s22.postimg.org/uq00qzslr/picyture.png</a></p>
  
  <p>Code:</p>
  
  <p>SampleServer:</p>
  
  <pre><code>import java.rmi.Naming;
  import java.rmi.RemoteException;
  import java.rmi.RMISecurityManager;
  import java.rmi.server.UnicastRemoteObject;
  
  public class SampleServer {
      public static void main(String args[]) {
          if (args.length != 1) {
              System.err.println("usage: java SampleServer rmi_port");
              System.exit(1);
          }
          // Create and install a security manager
          if (System.getSecurityManager() == null)
              System.setSecurityManager(new RMISecurityManager());
          try {
              // first command-line argument is the port of the rmiregistry
              int port = Integer.parseInt(args[0]);
              String url = "//localhost:" + port + "/Sample";
              System.out.println("binding " + url);
              Naming.rebind(url, new Sample());
              // Naming.rebind("Sample", new Sample());
              System.out.println("server " + url + " is running...");
          }
          catch (Exception e) {
              System.out.println("Sample server failed:" + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>SampleClient:</p>
  
  <pre><code>import java.rmi.Naming;
  import java.rmi.RemoteException;
  
  public class SampleClient  {
      public static void main(String args[]) {
          try {
              if (args.length &lt; 3) {
                  System.err.println("usage: java SampleClient host port string... \n");
                  System.exit(1);
              }
  
              int port = Integer.parseInt(args[1]);
              String url = "//" + args[0] + ":" + port + "/Sample";
              System.out.println("looking up " + url);
  
              SampleInterface sample = (SampleInterface)Naming.lookup(url);
  
              // args[2] onward are the strings we want to reverse
              for (int i=2; i &lt; args.length; ++i)
                  // call the remote method and print the return
                  System.out.println(sample.invert(args[i]));
          } catch(Exception e) {
              System.out.println("SampleClient exception: " + e);
          }
      }
  }
  </code></pre>
  
  <p>SampleInterface:</p>
  
  <pre><code>import java.rmi.Remote;
  import java.rmi.RemoteException;
  
  public interface SampleInterface extends Remote {
      public String invert(String msg) throws RemoteException;
  }
  </code></pre>
  
  <p>Sample:</p>
  
  <pre><code>import java.rmi.Remote;
  import java.rmi.RemoteException;
  import java.rmi.server.*;
  
  // this is the class with remote methods
  
  public class Sample
    extends UnicastRemoteObject
    implements SampleInterface {
  
      private int a;
  
      public Sample() throws RemoteException {
      System.out.println("New instance of Sample created");
      a = 1;
      }
  
      public String invert(String m) throws RemoteException {
          // return input message with characters reversed
          System.out.println("invert("+m+") a=" + a);
          return new StringBuffer(m).reverse().toString();
      }
  }
  </code></pre>
  
  <p>policy:</p>
  
  <pre><code>grant {
      permission java.security.AllPermission;
  };
  </code></pre>