Well i am getting exception in HelloClient
Exception is clientjava.security.AccessControlException: access denied (
java .net.SocketPermission 127.0.0.1:1099 connect,resolve)
I don’t get the exception if I remove the following lines.
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
What is the reason of it and why we set security manager .
Here is the code from wrox.
////////////////////////////////////////////
import java.rmi.*;
public interface HelloInterface extends Remote
{
public
String sayHello()throws RemoteException;
}
/////////////////////////////////////////////
import java.rmi.*;
import java.rmi.server.*;
public class HelloServer extends UnicastRemoteObject implements HelloInterface
{
public HelloServer()throws RemoteException
{
super();
}
public String sayHello()throws RemoteException
{
System.out.println("going to return Hello Rmi");
return "Hello Rmi";
}
}
/////////////////////////////////////////////////
import java.rmi.*;
import java.rmi.server.*;
public class RegisterIt
{
public static void main(String args[])
{
try{
HelloServer hello=new HelloServer();
System.out.println("Object instantiated of remote");
Naming.rebind("/HelloServer",hello);
System.out.println("Object Server in the registery");
}
catch(Exception e)
{
System.out.println("Exception is in register"+e);
}
}
}
///////////////////////////////////////////
import java.rmi.*;
public class HelloClient
{
public static void main(String args[])
{
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
try{
HelloInterface obj=(HelloInterface)Naming.lookup("/HelloServer");
System.out.println("object is"+obj);
String msg=obj.sayHello();
System.out.println("Message is"+msg);
}
catch(Exception e){
System.out.println("Exception is in client"+e);
}
}
}