Home > Archive > Java Help > January 2006 > control-C issue running binary
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
control-C issue running binary
|
|
| Mick Limprecht 2006-01-28, 7:05 pm |
| I have a java command line program that runs another linux binary
executable as one of it's functions.
The binary runs fine. Here is how it gets done.
I have a collection of environment variables set in a an
array I use at runtime along with a command line string.
Call runtime.exec.
-------------------------------------------
// Set up the runtime.
Runtime runtime = Runtime.getRuntime();
try {
// Convert ArrayList to String Array for Process.
envArray = (String[])envA.toArray(new String[0]);
// ---- RUN THE SIM ----
Process p = runtime.exec(cmdine, envArray);
// Using a buffered reader now. Seems to work better.
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitStatus = p.waitFor();
// Might need to change this to just the number.
System.out.println("Exit Value: " + exitStatus);
if (exitStatus == 1) { // Error in runtime.
System.out.println("\nERROR:");
BufferedReader errorout = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
}// if (exitStatus == 1)
} catch (Exception e) {
System.out.println("Process P Exception = " + e.getMessage());
}
-------------------------------------------
This all works fine. The problem I have is the binary has it's own
command line interface so when a control-C is entered while the binary
is running it will stop (not exit) what it is doing at that point so
you can interact with program through it's own command line (set
variables, change parameters, step, etc).
What happens when I run the binary under the java app the control-C
exits the program rather than just stop at the programs internal
command line interface.
I'm thinking the Java program sees the control-C and takes that as
an exit instead of letting the binary see it. If that's true I'm not
sure how to pass on the control-C. Intercept through the Stream?
Then what?
Any ideas on how I can get the control-C to behave the correct way?
Thanks in advance,
Mick
| |
| Gordon Beaton 2006-01-29, 7:01 pm |
| On Sun, 29 Jan 2006 00:02:25 GMT, Mick Limprecht wrote:
> I have a java command line program that runs another linux binary
> executable as one of it's functions.
[...]
> This all works fine. The problem I have is the binary has it's own
> command line interface so when a control-C is entered while the binary
> is running it will stop (not exit) what it is doing at that point so
> you can interact with program through it's own command line (set
> variables, change parameters, step, etc).
When you type ctrl-c in a console where an application is running, the
program does not "read a ctrl-c character", so there's nothing you can
send through the stream to cause the same effect.
What happens is that ctrl-c is interpreted by the console driver,
which then sends a signal (usually SIGINT) to all of the processes in
the process group of the foreground process.
You can do something similar with "kill -INT <pid>" or "kill -INT
<process group>". Read the man page for kill.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
|
|
|
|
|