You are not logged in.
Pages: 1
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
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
I used something called shutdown hooks, i think maybe on the Runtime object to use ^C in a project i did this semester.
Offline
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
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
Perhaps this will help? http://www.dclausen.net/javahacks/signal.html
Offline
Looks promising, I'll post a solution soon if it works.
Offline
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
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
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
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
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
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
Pages: 1