• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Terminating program

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the program to exit, just return from the main() method.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How bout using System.exit(0)?

John
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic