Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

control-C issue running binary

Thread view: 
Mick Limprecht - 29 Jan 2006 00:02 GMT
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 - 29 Jan 2006 16:18 GMT
> 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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.