LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2006, 12:21 PM   #1
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
are there any ways to catch ctrl-c in a console java program ??


i have been looking for ways to catch ctrl-c in a console java program but still cant find any ...

is it possible at all ??
preferably doing it inside the java program itself ...

//not really urgent ...
//thanks in advance ...


.

Last edited by alred; 07-17-2006 at 12:33 PM.
 
Old 07-17-2006, 02:03 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
google for "java signal handling".
 
Old 07-17-2006, 02:05 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
You can sorta do it w/ the Runtime method, addShutdownHook. This will be called whether it's exited normally or w/ Ctr-C though, so you may want to add some way to check.


Code:
import java.lang.System;
import java.lang.Runtime;

public class A {
    public static void main(String args[]) {
	Runtime.getRuntime().addShutdownHook(new Thread() {
		public void run() {
		    System.out.println("Exited!");
		}
	    });
	for(;;);
    }
}
Compile, then run this, and type Ctr-C while it's running. It will print Exited! to the terminal... at least it should
 
Old 07-17-2006, 02:23 PM   #4
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658

Original Poster
Blog Entries: 8

Rep: Reputation: 31
great !! thanks to both of you ...


//java is great ...

.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
different ways of getting $COLUMNS in a C program Y0jiMb0 Programming 14 02-21-2006 06:01 PM
catch Ctrl-C from shell script gecoool Programming 4 10-27-2005 01:19 AM
C++ catch ctrl+c true_atlantis Programming 1 10-01-2004 11:23 AM
Java: try won't catch exception nro Programming 3 09-15-2004 11:28 PM
Ways to run a program... RichardSimmons Linux - Newbie 4 03-25-2004 02:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration