4

I have a button in my program that, when pressed, is supposed to take you to my wiki page about the program. I used the following line to do so:

java.awt.Desktop.getDesktop().browse(new java.net.URI("http://supuh.wikia.com/wiki/BHT"));

The problem is that, no matter what environment in which the program is run, I always get the following error:

java.security.AccessControlException: access denied (java.awt.AWTPermission showWindowWithoutWarningBanner)

does anyone know how I can fix this? Note that this only works in the one program. Any other program I make can use the same method with no problem.

Exit hook


At the start of my program, this hook is added. The program runs fine without it...

System.setSecurityManager(new SecurityManager()
{
 @Override
 public void checkExit(int status)
 {
 closeFile(status);
 }
});

this hook is needed, but the browse(URI uri) method in question won't work with it. Solutions?

Bozho
599k147 gold badges1.1k silver badges1.2k bronze badges
asked Nov 10, 2010 at 15:19
1
  • I removed the not-relevant bits of code from the question to make it more clear. There was one unused instantiation of SecurityManager in your listener - get rid of it (for the sake of cleaner code) Commented Nov 15, 2010 at 7:37

2 Answers 2

7
+50

This means you are running with a security manager:

SecurityException - if a security manager exists and it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess; and not invoked from within an applet or Java Web Started application

If this is an applet, or a Java Web Start app - sign your jar.

Update Adding a security manager to detect program exit is wrong. There are multiple ways to do this properly. In your case I guess this would be most appropriate:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
 @Override
 public void run() {
 closeFile();
 }
));

Swing-specific solutions are:

  • if you don't have to perform extra actions, use frame.setDefaultCloseAction(Frame.EXIT_ON_CLOSE)

  • use addWindowStateListener and check for WindowEvent.WINDOW_CLOSED

That said, two notes:

  • you must not hold files open for a long time. Use try/catch/finally to open and close them whenever they are needed.

  • if you really need a security manager at some point, make sure you override the appropriate method of the SecurityManager that checks whether you can open the link. (won't tell you which one, so that you are not tempted to jump onto this solution, which is wrong)

To summarize, I'd go for setDefaultActionOnClose, and close each file right after I finish reading/writing it.

Update 2: After you linked to your original question describing what exactly are you trying to achieve, things change a bit. You are trying to prevent exit, so you do need a SecurityManager. This makes it so that you should override the checkPermission method and do nothing there (i.e. don't throw exceptions), at least when these permissions are checked (they are checked when browse is called):

  • new AWTPermission("showWindowWithoutWarningBanner")
  • new FilePermission("<<ALL FILES>>", SecurityConstants.FILE_EXECUTE_ACTION)

Update 3 Here's how exactly to override the method:

@Override
public void checkPermission(Permission permission) {
 if (permission instanceof AWTPermission) {
 if (permission.getName().equals("showWindowWithoutWarningBanner")) {
 return;
 }
 }
 
 if (permission instanceof FilePermission) {
 if (permission.getActions().equalsIgnoreCase("execute")) {
 return;
 }
 }
 java.security.AccessController.checkPermission(permission);
}

(you can go without the outer if-s)

Update 4 The above method will work only if you have given permissions to your program. Otherwise it is a not-well documented behaviour of the JVM that overriding security managers are not allowed to be unprivileged. Take a look at this report - the comments say how to work it around.

To make your life simpler, you can simply @Override public void checkPermission(..) with an empty method body.

answered Nov 10, 2010 at 15:23
Sign up to request clarification or add additional context in comments.

28 Comments

I am running it as a desktop app, but as a jar or as a class file it still throws it.
how exactly do you start your application? Because I just ran one here, and it browsed successfully.
I have started it through an IDE, in a jar, through a shortcut to the jar, and in the command prompt.
Try starting it from a simple class, not a jar, for a test. in my IDE it worked.
Then your IDE is running a security manager, or your JRE is somehow guarded by one (don't know whether this is possible). The normal installation does not behave like that.
|
0

Instead of using your own SecurityManager, install a shutdown hook instead:

Runnable runnable = new Runnable() {
 closeFile(status);
}
Runtime.getRuntime().addShutdownHook(new Thread (runnable, "Close file"));
answered Nov 19, 2010 at 14:10

2 Comments

The problem, now, is how do I get the "status" integer variable of exiting?
the problem seems more complicated - in the countless comments on my answer he posted a link to his original problem, so a security manager is due.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.