• Post Reply Bookmark Topic Watch Topic
  • New Topic

Terminating program  RSS feed

 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel silly asking this..but ive never tried this before so i'll ask.
Im writing a code for an assignment..but the program has to terminate when i press the enter button...how can i do that?

oh and its a simple code with just a public static void main [] method so you can understand why i feel a bit sheepish for asking.
Thanks
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just giving you one hint. There is one class System defined in java.lang package. Ok Here is its documentation. Just go through it and let us know is it of any use?

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html


- Naseem
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the program to exit, just return from the main() method.

 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How bout using System.exit(0)?

John
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.exit() will indeed exit your program, but it is much better to exit without doing so. Only use System.exit() for those rare circumstances when no other way of exiting will work. Such situations ought not to come up when you are a beginner, so beginners should never be using System.exit().

To make a Java program exit cleanly, you simply need to arrange for all its non-daemon threads to return. In the case of a simple, single-threaded program, that just means returning from main().
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter -- it's a GUI program: he mentions "pressing a button". In that case using the System.exit() method is by far the easiest thing to do.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Peter -- it's a GUI program: he mentions "pressing a button". In that case using the System.exit() method is by far the easiest thing to do.


... unless the program later grows into something more advanced, which does background processing, or requires clean-up on shut-down etc. In which case, System.exit() will stuff it up nicely.

It's not exactly difficult to get a GUI program to exit nicely, without System.exit(). All you need to do is (a) arrange for the code to return when you press the Exit button, and (b) ensure your main window has a parent that you create, and dispose when you're about to exit.

You're right that using System.exit() is an easy way to implement an Exit button in a simple GUI application. I'm hoping to explain why it's not something to get into the habit of.
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Let me know What other could be the ways to terminate from an Action Listner on Button other then System.exit();. I am not fimiliar with other method in such a case.

Thanks...

--Jamshaid..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!