javarmihostsrmiregistry

RMI connection refused


I am trying to get a rmi connection going. I have ran into many security issues but have been unable to find a way past all this. I execute my jar file with:

java -Djava.security.policy=java.security.AllPermission -jar "myjarfile"

The code I have been using to create this is:

public class server
{
    public static void main(String args[])throws Exception
    {
    if (System.getSecurityManager() == null)
        System.setSecurityManager ( new RMISecurityManager() {
        public void checkConnect (String host, int port) {}
        public void checkConnect (String host, int port, Object context) {}
        });

    try
    {
        sampleserverimpl server = new sampleserverimpl();
        System.out.println("SERVER IS WAITING");
        LocateRegistry.createRegistry(2020);

        //Runtime.getRuntime().exec("rmiregistry 2020");
        Naming.rebind("//localhost:2020/SERVER", server);

    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
};

The error trace I am receiving is:

Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlExcept
ion: access denied (java.net.SocketPermission 127.0.0.1:31199 accept,resolve)jav
a.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:

    java.io.EOFException

I have tried different ways to get around this, can anyone see the issue here?

Thanks


Solution

  • -Djava.security.policy accepts a URL which points to a policy file which in turn contains the permissions. So you should have: -Djava.security.policy=/some/path/my.policy as the JVM argument where the my.policy file contains:

    grant {
      permission java.security.AllPermission;
    };
    

    Also, in order to avoid the NULL check present in your code and the manual creation of a SecurityManager, you can request a SecurityManager be automatically installed for your application by passing the JVM switch: -Djava.security.manager.

    Your final JVM invocation should look like:

    java -Djava.security.manager -Djava.security.policy=/some/path/my.policy