You are not logged in.

#1 2011-07-20 02:44:54

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Override ^C and ^4 in Java

I am trying to write a program in Java, which runs in a terminal emulator (I use Linux Virtual Terminal), where <Control>C and <Control>4 should be used;
but when ^C is pressed the program exits, and when ^4 is pressed a Java prints out a process dump. This happens even if I executes the program by running:

java [stuff] < [any device] 2> /dev/null

And using  ⁅new FileInputStream(new File("/dev/stdout"))⁆  as the input stream (instead of /dev/stdin).
This means that stdin and stdout is not the same device, but Java still picks up ^C and ^4 from stdout.

Any ideas what to do? Of course the solution be run on same tty as it was started on, and the using should not need to specify the tty manually.

I can imagine that the ^C issue is not necessarily built in to Java; if so, there is no problem running C code or program using the same tty (like with stty).

Offline

#2 2011-07-21 00:30:31

coffee_addict
Member
From: Canada
Registered: 2011-06-14
Posts: 12

Re: Override ^C and ^4 in Java

Pressing ^C sends the signal SIGINT to the JVM, and the default behaviour is to terminate.
I'm not too familiar on how to handle signals in java, but searching for "java signal handling" should get you
headed in the right direction.

Offline

#3 2011-07-21 17:25:17

Diaz
Member
From: Portugal
Registered: 2008-04-16
Posts: 366

Re: Override ^C and ^4 in Java

I used something called shutdown hooks, i think maybe on the Runtime object to use ^C in a project i did this semester.

Offline

#4 2011-07-22 02:12:33

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Override ^C and ^4 in Java

Shutdown hooks won't work because the application will still shut down after they run (I think; not a Java expert).  And ^4 is actually ^\, which sends SIGQUIT I believe.

Offline

#5 2011-07-22 14:13:51

Diaz
Member
From: Portugal
Registered: 2008-04-16
Posts: 366

Re: Override ^C and ^4 in Java

Yes that's true, shutdown hooks will eventually stop the application. I don't think we can handle signals in java, but if anyone finds how please tell, it could be useful ^^

Offline

#6 2011-07-22 14:50:28

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Override ^C and ^4 in Java

Offline

#7 2011-07-28 11:14:50

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

Looks promising, I'll post a solution soon if it works.

Offline

#8 2011-07-29 01:32:30

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

Do anyone know how to generate a .h file from .so file.
»nm file.so» lists prototypes, but I want to generate a .h, it could take a while to do that by hand.

(Yes, this is a part of the solution candidate... I need to modify final variables :Þ.)

Offline

#9 2011-08-03 07:24:44

vadmium
Member
Registered: 2010-11-02
Posts: 63

Re: Override ^C and ^4 in Java

Hibernate wrote:

I can imagine that the ^C issue is not necessarily built in to Java; if so, there is no problem running C code or program using the same tty (like with stty).

I’m no expert on Java and I don’t know if Java has an API for it directly, but it sounds like “stty -isig” is what you want. My understanding of how it works is: Control+C generates ASCII \x03 and Control+4 (normally Control+Backslash) generates ASCII \x1C characters into the terminal. Linux normally intercepts those control characters and sends interrupt and quit signals instead, to a process associated with the terminal (I’m not 100% clear on the details here). But that ISIG terminal flag prevents the interception. See stty and termios manual pages.

Alternatively I think you should be able to enter those characters by prefixing them with Control+V.

Offline

#10 2011-08-03 11:44:53

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

stty -isig works, thank you, so much less work than hacking Java without modifying its .jar files.
And prepending ^V does not seem to work, even if that is not even a solution that you always can use.

But ^\, is an extra key on my keyboard layout, so why is that default when ^4 is always 2 keys?


So the easy way of allowing ^C and ^4 is by using the following code with 'on' as 'false' and 'on' as 'true' when restoring the sig-functionality:

final String tty = (new File("/dev/stdout")).getCanonicalPath();
(new ProcessBuilder("/bin/sh", "-c", "stty " + (on ? "isig" : "-isig") + " < " + tty + " > /dev/null")).start();

Offline

#11 2011-08-03 11:46:25

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

Maybe > should be 2> (or both), but I so not think an exception will ever be thrown if you are using a TTY.

(Yes I forgot that I could edit the post…)

Last edited by Hibernate (2011-08-03 11:47:47)

Offline

#12 2011-08-05 19:49:26

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

tavianator wrote:

Shutdown hooks won't work because the application will still shut down after they run (I think; not a Java expert).  And ^4 is actually ^\, which sends SIGQUIT I believe.

Yes, it sends SIGQUIT, but Java seems not to care, makes a dump and keep on working.

Offline

#13 2011-08-05 19:53:48

Hibernate
Member
From: Stockholm, Sweden
Registered: 2011-07-12
Posts: 100

Re: Override ^C and ^4 in Java

tavianator wrote:

Shutdown hooks won't work because the application will still shut down after they run (I think; not a Java expert).

If so, isn't it possible to just make a sleeping forever loop. (Yes, that is ugly.)

Offline

Board footer

Powered by FluxBB