title: Java RMI AccessControlException: access denied
body: <p>Hey I'm getting a <code>AccessControlException: access denied</code> when  attempting to start up a RMI app I'm writing, I can't work out why I get this exception if I open it on the default port 1099, or on another dynamic port, my policy file currently grants everything (will change when app is finished).</p>
  
  <p>I am stuck as to where it is going wrong, any help would be of great use</p>
  
  <p><strong>My code</strong></p>
  
  <pre><code>public class Main {
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws RemoteException, AlreadyBoundException, MalformedURLException {
       if (System.getSecurityManager() == null)
       {
          System.setSecurityManager ( new RMISecurityManager() );
       }
  
       CreditCardServer ccs = new CreditCardServer();
  
       int port = 1099;
  
       try {
          port = Integer.valueOf(args[0]);
          }
       catch (Exception e)
          {
          System.out.println("Invlaid Port");
          }
  
       if (((port &lt;= 65535) &amp;&amp; (port &gt;= 49152)) || port ==1099)
       {
       System.out.println("Valid Port");
       }
       else
       {
           port = 1099;
          System.out.println("Port not in Dynamic Range 49152&lt;--&gt;65535");
       }
  
       System.out.println(port);
  
       LocateRegistry.createRegistry(port);
  
       LocateRegistry.getRegistry().bind("CreditCardServer", ccs);
  
       while (true)
       {
          //hum?
       }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>The Stack Trace</strong></p>
  
  <pre><code>vega3 [ia32.linux] 23% java -Djava.security.policy=wideopen.policy -jar "BookStore-CreditCardServer.jar 65000"
  </code></pre>
  
  <p>Valid Port</p>
  
  <p>65000</p>
  
  <pre><code>Exception in thread "main" java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
          at java.security.AccessControlContext.checkPermission(AccessControlContext.java:342)
          at java.security.AccessController.checkPermission(AccessController.java:553)
          at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
          at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
          at java.net.Socket.connect(Socket.java:536)
          at java.net.Socket.connect(Socket.java:492)
          at java.net.Socket.&lt;init&gt;(Socket.java:389)
          at java.net.Socket.&lt;init&gt;(Socket.java:203)
          at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
          at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
          at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
          at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
          at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
          at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
          at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
          at bookstorecreditcardserver.Main.main(Main.java:56)
  </code></pre>
  
  <p><strong>My Policy File</strong></p>
  
  <pre><code>grant {
  // Allow everything for now
  permission java.security.AllPermission;
  };
  </code></pre>