spinker
spinker

Reputation: 308

RMI access denied error all the time

i am having a problem creating RMIserver class because i keep getting this error : access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)

now i read on google something about cerating new policy file but i dont really understand how to do so, can please someone help me ?

here is my server code :

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*; 
import java.security.Permission;
import java.security.Security;

public class RmiServer extends UnicastRemoteObject 
    implements RmiServerIntf 
    {
    public static final String MESSAGE = "Hello world";

    public RmiServer() throws RemoteException
    {
    }

    public String getMessage()
    {
        return MESSAGE;
    }


    public static void main(String args[])
    {
        System.out.println("RMI server started");

        // Create and install a security manager
        if (System.getSecurityManager() == null)
        {
            System.setSecurityManager(new RMISecurityManager());
            System.out.println("Security manager installed.");
        }
        else
        {
            System.out.println("Security manager already exists.");
        }

        try
        { //special exception handler for registry creation
            LocateRegistry.createRegistry(1099); 
            System.out.println("java RMI registry created.");
        } 
        catch (RemoteException e)
        {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }

        try 
        {
            //Instantiate RmiServer
            RmiServer obj = new RmiServer();


            // Bind this object instance to the name "RmiServer"
            Naming.rebind("//localhost/RmiServer", obj);

            System.out.println("PeerServer bound in registry");
        } 
        catch (Exception e) 
        {
            System.err.println("RMI server exception:" + e);
            e.printStackTrace();
        }
    }

}    

Upvotes: 1

Views: 4218

Answers (2)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77485

You really need to read the manual of RMI, and how to create a policy file:

http://docs.oracle.com/javase/tutorial/rmi/running.html

Upvotes: 0

user207421
user207421

Reputation: 311050

You are using a SecurityManager (why?) but your security policy doesn't grant you the permission specified in the exception. You don't need a SecurityManager at all unless you are planning to use the RMI codebase feature: are you?

Upvotes: 2

Related Questions