0

I have a application. On Close button click I am calling System.exit(0). But on a machine it is not getting close that application. It is happening on few systems not all.

I read some docs and forums and found that the exit() with 0 (zero) int value is used for normal exit. and normally this method never returns normally (means close the application). BUT if a security manager exists and its checkExit method doesn't allow exit with the specified status.

My application is not creating any Security Manager and I know also that By default an application does not have a security manager. That is, the Java runtime system does not automatically create a security manager for every Java application. So by default an application allows all operations that are subject to security restrictions.

I am also not sure that it is happening due to security manager.

Now I want to know two things.

  1. Can I close the application successfully with the existence of system manager, How ?

  2. Does System.setSecurityManager(null); is the right way to do this ?

I have to close my application by any way. Any suggesstion ?

Thanks

Tej Kiran

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
  • 4
    Calling System.exit(0) should be the right way, no doubt. What means "is not getting close"? Is there an exception? Is just happening nothing? Are you sure System.exit(0) is called? Post code please. – Thomas Uhrig Apr 30 '12 at 07:58
  • Perhaps your app launches some non daemon threads that are stopping the JVM from terminating? – henry Apr 30 '12 at 08:05
  • 2
    I'm pretty sure calling `System.exit(0)` will ignore any non-deamon threads.. Are you sure the button click is actually executing the code? – Paaske Apr 30 '12 at 08:12
  • @Frank just checked with some code rather than reading the web, looks like that's one falls into the Java urban myth category and that a thread won't prevent the JVM from terminating :-) – Nick Holt Apr 30 '12 at 08:40
  • Yes I am sure that System.exit(0) is calling. because immediate before I have a debug statement "Doing exit from application". and that is printed. – Tej Kiran Apr 30 '12 at 09:23
  • My class is extends JFrame and I have added WindowsActinListener here and put System.exit(0) in windowClosing(WindowEvent e) method. – Tej Kiran Apr 30 '12 at 09:37
  • @NickHolt A thread **will** prevent JVM from terminating if the thread executing `main` ends by virtue of `main` method completing. That's the scenario where any other non-daemon threads will keep Java running. – Marko Topolnik Apr 30 '12 at 09:52
  • @TejKiran Use `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` -- that's the idiomatic approach. – Marko Topolnik Apr 30 '12 at 09:54
  • Yes It is correct @Marko, but I am also saving some settings before closing the GUI. So I can not close GUI by DefaultCloseOperation(). My Question is that can I setSecuriyManager by Null. What does that affect ? Normally my code is working. but on some system it is not working so using this I can close my application. – Tej Kiran Apr 30 '12 at 10:02
  • I think that once the `SecurityManager` is set, it cannot be unset (that kind of `SecurityManager` would be pointless as a security measure). If setting it to `null` succeeds, I'd guess there wasn't one there to begin with. – Marko Topolnik Apr 30 '12 at 10:09
  • Do you know if there are any shutdown hooks registered in your application, or if any of the libraries you are using that has a shutdown hook? The shutdown hook is a thread, and if there is a deadlock in that thread causing it to never terminate, the JVM will never exit. – Paaske Apr 30 '12 at 13:24

0 Answers0