In perl, we can do this:
print("\x03");
to do Ctrl+C
Is there something similar we can do in Java?
In perl, we can do this:
print("\x03");
to do Ctrl+C
Is there something similar we can do in Java?
In perl, we can do this : [...]
Really? Did you try and it terminated any program?
This example
#!/usr/bin/perl
print "\x03";
print "\n";
print "Hello World!\n";
Will print
<funny character>
Hello World!
Piping the output to some other program will also not terminate any of them.
You want to send the ^C signal. But you really just send the 0x03 byte. If you want to terminate someone you have to use something like this (OS dependend):
kill(process_id_of_destination, SIGTERM);
The confusion's source is, that in a normal terminal window the OS intercepts the ^C key and translates it into the TERM
signal. But the program will not receive that ^C on its input stream, it will receive the signal and it might process it in some registered hook. So if you write ^C yourself it is just plain data and will not be processed specially.
Addendum
The following text applies to Unixoids/Linux only, but most probably not to Windows. I think that's OK, because telnet
, perl
, tail -f
indicate Unix background.
As already mentioned: sending ^C (aka. the byte 0x03) to the standard input of a process will not trigger any signal. Usually. But the terminal window itself and some other programs like rlogin, telnet, sshd must pretend, that the stuff running within then is connected directly to a terminal. This includes the transformation of some key combos into some signals. This is done using "pseudo terminals" (aka. pty or pts). For details on this stuff lookup man 4 pts
and man 7 pty
and/or read the Unix book of Richard Stevens.
The excerpt for your case is: Data written to the "master PTY" will be handled exactly like keyboard input. So a ^C written into the master will be translated into a signal which is delivered to the process reading the "slave PTY". So if your print("\x03");
in Perl works and the Java version System.out.println('\3');
doesn't, then I assume that Perl is somehow set up differently and writes to a PTY master and Java writes only to a plain pipe.