Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

replaced http://bugs.sun.com/bugdatabase/view_bug.do with https://bugs.java.com/bugdatabase/view_bug
Source Link

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 this report - the comments say how to work it around.

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.

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.

Bounty Awarded with 50 reputation awarded by Ky -
added 483 characters in body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
@Override
public void checkPermission(Permission permission) {
 if (permission instanceof AWTPermission) {
 if (permission.getName().equals("showWindowWithoutWarningBanner")) {
 return;
 }
 }
 
 if (permission instanceof FilePermission) {
 if (permission.getActions().equalIgnoreCaseequalsIgnoreCase("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.

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

(you can go without the outer if-s)

@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.

added 612 characters in body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k

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().equalIgnoreCase("execute")) {
 return;
 }
 }
 java.security.AccessController.checkPermission(permission);
}

(you can go without the outer if-s)

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)

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().equalIgnoreCase("execute")) {
 return;
 }
 }
 java.security.AccessController.checkPermission(permission);
}

(you can go without the outer if-s)

added 594 characters in body; edited body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
Loading
added 1 characters in body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
Loading
added 320 characters in body; added 217 characters in body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
Loading
added 559 characters in body
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
Loading
Source Link
Bozho
  • 599.1k
  • 147
  • 1.1k
  • 1.2k
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /