Problems in executing a process

Discussion in 'Java' started by asd, Jun 17, 2005.

  1. asd

    asd Guest

    Hi all,

    I am trying to execute a cpp application (called iosconvert) from java
    code. The details are as follows:

    Runtime rtime = Runtime.getRuntime();

    String str_ios = new String[]("iosconvert", "/home/arvind/cisco2.conf",
    "/home/arvind/nct1/bin/cisco2.xml");

    proc = rtime.exec(str_ios);

    int exitVal = proc.waitFor();

    System.out.println("exit = "+exitVal);

    Iosconvert takes two parameters, which are passed. Now when I run this
    code it hangs at proc.waitFor().

    Since I am executing the code on CLI I have to do a CTRL+C. The exit
    value that gets printed is 129.

    On the other hand when I execute the cpp application directly from CLI
    it works fine.

    Thanks in advance for any help.
    I am using j2re 1.4.2 on Linux env.

    regards,
    ASD
    asd, Jun 17, 2005
    #1
    1. Advertising

  2. On 17 Jun 2005 01:00:53 -0700, asd wrote:
    > I am trying to execute a cpp application (called iosconvert) from
    > java code. The details are as follows:


    [...]

    > Now when I run this code it hangs at proc.waitFor().


    Because you are not reading the output produced by the child process,
    its output streams block and the process is prevented from running to
    completion.

    Either consume the contents of the output and error streams while the
    process is running, or specify the command so that no output is sent
    to these streams (by specifying a flag to isoconvert, or using a shell
    and redirection).

    /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
    Gordon Beaton, Jun 17, 2005
    #2
    1. Advertising

  3. asd

    asd Guest

    >Either consume the contents of the output and error streams
    How do I do this?

    Please help me out.

    regards,

    ASD
    asd, Jun 17, 2005
    #3
  4. On 17 Jun 2005 01:59:52 -0700, asd wrote:
    >>Either consume the contents of the output and error streams

    > How do I do this?


    Runtime.exec() returns a Process object. Obtain the output and error
    streams from that.

    Read each of those streams until you reach EOF. Note that you need to
    read both streams concurrently, so you will need an additional thread
    for one of them.

    If you *know* that the process will not write anything to its error
    stream, you can cheat a little, and not read from that stream. That
    also removes the need for the additional thread.

    Finally, after reaching EOF on the streams, wait for the process to
    terminate and get the exit value.

    A simpler (but platform specific) solution is to let a shell do the
    work for you. Specify the command line as an array of 3 Strings, like
    this:

    String[] cmd = {
    "/bin/sh",
    "-c",
    "iosconvert /home/arvind/cisco2.conf /home/arvind/nct1/bin/cisco2.xml"
    + " > /dev/null 2>&1"
    }

    It's important to group the command into 3 Strings exactly the way
    I've shown here (I broke the last one only to avoid line wrapping in
    my post).

    /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
    Gordon Beaton, Jun 17, 2005
    #4
  5. asd

    John Currier Guest

    Didn't they implement something in 1.5 so you don't have to go though
    that mess?

    John
    John Currier, Jun 17, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cédric Rossé
    Replies:
    3
    Views:
    22,634
    Anmol_Kumar
    Sep 23, 2008
  2. Buddy Ackerman
    Replies:
    1
    Views:
    370
    Bruce Barker
    May 11, 2005
  3. Pratibha

    Process is not executing

    Pratibha, May 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    Pratibha
    May 26, 2005
  4. Marcel van den Hof
    Replies:
    3
    Views:
    578
    Bruce Barker
    Aug 12, 2005
  5. jpock76
    Replies:
    0
    Views:
    598
    jpock76
    Aug 23, 2010
Loading...

Share This Page