Search code examples
javasecurityrmipolicy

Java RMI / access denied


I am trying to create an RMI server, but i get the following exception at runtime:

java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:4555" "listen,resolve")

I have read the Oracle tutorial about policy files, here's the content of my server.policy file which i inserted into the src folder:

grant codeBase "file:///C:/EclipseProjects/MultiServiceServer/src"{

permission java.security.AllPermission;

};

I also set VM arguments to:

-Djava.security.policy=src/server.policy

Finally, here is the code of this simple RMI server:

public class Main {

public static void main(String[] args)
{       

    System.out.println("Server is running..");

     if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
     }

    try {

        String name = "AvailableService";

        AvailableService service = new ServiceList();

        AvailableService stub = (AvailableService)UnicastRemoteObject.exportObject(service, 0);

        LocateRegistry.createRegistry(4555);

        Registry reg = LocateRegistry.getRegistry(4555);

        reg.bind(name, stub);

        System.out.println(name+" bound");

    } catch(Exception e){e.printStackTrace();}


}

Thanks for your help.


Solution

  • The C:/EclipseProjects/MultiServiceServer/src folder won't be there at runtime, and doesn't contain .class files at compile time. Granting it any permissions at all is therefore futile. You don't need to use a security manager in RMI at all unless you're using the codebase feature. which you aren't. So remove the security manager.