access denied (java.lang.RuntimePermission createSecurityManager)

S

Shishir

Hey,
I am a beginner to learning RMI and hence was trying this simple
program.

here is my code with the main class. The error is shown in this class:


import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;


@SuppressWarnings("serial")
public class HelloImpl extends UnicastRemoteObject
implements Hello {

public HelloImpl() throws RemoteException {
super();
}

public String sayHello() {
return "Hello World!";
}

public static void main(String args[]) {

// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();

// Bind this object instance to the name "HelloServer"
System.setSecurityManager (new RMISecurityManager() {
public void checkConnect (String host, int port) {}
public void checkConnect (String host, int port, Object
context) {}
});
Naming.rebind("HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}



This is the error that I get:


java.security.AccessControlException: access denied
(java.lang.RuntimePermission createSecurityManager)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.<init>(Unknown Source)
at HelloImpl$1.<init>(HelloImpl.java:30)
at HelloImpl.main(HelloImpl.java:30)
HelloImpl err: access denied (java.lang.RuntimePermission
createSecurityManager)




Any Help on this would be great.

Thanks a lot.
Shishir
 
R

Rhino

Shishir said:
Hey,
I am a beginner to learning RMI and hence was trying this simple
program.

here is my code with the main class. The error is shown in this class:


import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;


@SuppressWarnings("serial")
public class HelloImpl extends UnicastRemoteObject
implements Hello {

public HelloImpl() throws RemoteException {
super();
}

public String sayHello() {
return "Hello World!";
}

public static void main(String args[]) {

// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();

// Bind this object instance to the name "HelloServer"
System.setSecurityManager (new RMISecurityManager() {
public void checkConnect (String host, int port) {}
public void checkConnect (String host, int port, Object
context) {}
});
Naming.rebind("HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}



This is the error that I get:


java.security.AccessControlException: access denied
(java.lang.RuntimePermission createSecurityManager)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.<init>(Unknown Source)
at HelloImpl$1.<init>(HelloImpl.java:30)
at HelloImpl.main(HelloImpl.java:30)
HelloImpl err: access denied (java.lang.RuntimePermission
createSecurityManager)

Any Help on this would be great.

I'm not very knowledgeable about security but I _think_ this tutorial should
help:
http://java.sun.com/docs/books/tutorial/security1.2/index.html

If it doesn't, you could try comp.lang.java.security although I suspect they
focus on more advanced security issues.
 
T

Thomas Fritsch

Shishir schrieb:
[...]
public static void main(String args[]) {

// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();

// Bind this object instance to the name "HelloServer"
System.setSecurityManager (new RMISecurityManager() { This is line 30, isn't it?
public void checkConnect (String host, int port) {}
public void checkConnect (String host, int port, Object
context) {}
}); [...]

This is the error that I get:

java.security.AccessControlException: access denied
(java.lang.RuntimePermission createSecurityManager)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.<init>(Unknown Source)
at HelloImpl$1.<init>(HelloImpl.java:30)
at HelloImpl.main(HelloImpl.java:30)
HelloImpl err: access denied (java.lang.RuntimePermission
createSecurityManager)
You call System.setSecurityManager(...) two times. The first call gives
no error. The second call throws the exception, from inside the
constructor of SecurityManager. The reason is that the first
SecurityManager instance forbids creating a second SecurityManager instance.
(You might want to look into the API doc
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SecurityManager.html#SecurityManager()>
or directly into Sun's source of java.lang.SecurityManager now)

One simple solution for your problem is:
omit your first call System.setSecurity(...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top