Calling external program in java code

Discussion in 'Java' started by hrn, Sep 8, 2009.

  1. hrn

    hrn Guest

    Hi,

    I have problems with this code fragment.
    ..
    ..
    ..
    String exe = "external1.exe";
    String exe2 = "external2.exe";
    Process rt = Runtime.getRuntime().exec(exe1);
    rt.waitFor();
    System.out.println("Finished executing external program");
    ..
    ..
    When I run this code, it runs perfectly, but when I changed third line
    to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
    It holds at fourth line [ rt.waitFor(); ] and fifth line is never
    executed. Windows Task Manager shows that external2.exe is still
    running. When I call both external1.exe and external2.exe from command
    line, they run and stop in expected way and they don't need any user
    intervention to work and stop at the end. I need help.

    Thanks in advance.
     
    hrn, Sep 8, 2009
    #1
    1. Advertising

  2. hrn

    Daniel Pitts Guest

    hrn wrote:
    > Hi,
    >
    > I have problems with this code fragment.
    > ..
    > ..
    > ..
    > String exe = "external1.exe";
    > String exe2 = "external2.exe";
    > Process rt = Runtime.getRuntime().exec(exe1);
    > rt.waitFor();
    > System.out.println("Finished executing external program");
    > ..
    > ..
    > When I run this code, it runs perfectly, but when I changed third line
    > to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
    > It holds at fourth line [ rt.waitFor(); ] and fifth line is never
    > executed. Windows Task Manager shows that external2.exe is still
    > running. When I call both external1.exe and external2.exe from command
    > line, they run and stop in expected way and they don't need any user
    > intervention to work and stop at the end. I need help.
    >
    > Thanks in advance.
    >

    is it possible that external2.exe is either waiting for input, or
    outputting more data than external1?

    There is a finite buffer used for Process's output/error streams, so you
    *must* drain them (using a separate thread is a common practice, but not
    necessary). If the output or error stream become full, the process will
    block until they are drained.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Sep 8, 2009
    #2
    1. Advertising

  3. hrn

    hrn Guest

    On windows command line, any user input is not necessary after running
    external1 and external2. For both of them, I just write their names
    and their arguments (these arguments are names of files that will be
    processed) on command line and push enter button, then I do nothing,
    they work and stop succesfully. Although, I write the same (what I
    write on command line) in java as exe and exe2, exe works succesfully,
    exe2 waits.
     
    hrn, Sep 8, 2009
    #3
  4. On Tue, 08 Sep 2009 07:15:45 -0700, hrn wrote:

    > Hi,
    >
    > I have problems with this code fragment. .
    > .
    > .
    > String exe = "external1.exe";
    > String exe2 = "external2.exe";
    > Process rt = Runtime.getRuntime().exec(exe1); rt.waitFor();
    > System.out.println("Finished executing external program"); .
    > .
    > When I run this code, it runs perfectly, but when I changed third line
    > to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
    >

    As written, a class containing these fragments ought not to compile, let
    alone run because exe1 is never declared. Has anything else been changed
    or omitted?

    Create an SSCE that does compile and run and post that. A similar SSCE
    for each of the called programs would be even better if it can
    demonstrate the problem.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Sep 8, 2009
    #4
  5. hrn

    hrn Guest

    Actually, in my java program, exe is exe1. 1 is missing here. There
    has been a mistake while writing.
     
    hrn, Sep 8, 2009
    #5
  6. On Tue, 08 Sep 2009 13:45:17 -0700, hrn wrote:

    > Actually, in my java program, exe is exe1. 1 is missing here. There has
    > been a mistake while writing.


    Use copy & paste next time. However, a compiled and tested SSCE would be
    better.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Sep 9, 2009
    #6
  7. hrn

    Roedy Green Guest

    On Tue, 8 Sep 2009 07:15:45 -0700 (PDT), hrn <>
    wrote, quoted or indirectly quoted someone who said :

    >.
    >String exe = "external1.exe";
    >String exe2 = "external2.exe";
    >Process rt = Runtime.getRuntime().exec(exe1);
    >rt.waitFor();
    >System.out.println("Finished executing external program");


    You have constipation. You must allow for the flow of output from the
    child. See http://mindprod.com/jgloss/exec.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "I mean, source code in files; how quaint, how seventies!"
    ~ Kent Beck (born: 1961 age: 48), evangelist for extreme programming.
     
    Roedy Green, Sep 9, 2009
    #7
    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. Rey
    Replies:
    4
    Views:
    11,013
    Roedy Green
    Dec 12, 2003
  2. Jon Slaughter

    Calling external program in C++

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    9
    Views:
    25,864
  3. Juggler

    calling external program from within C++

    Juggler, Feb 24, 2005, in forum: C Programming
    Replies:
    5
    Views:
    588
    Nils Weller
    Feb 24, 2005
  4. Jeff
    Replies:
    13
    Views:
    1,155
    vanilla
    Oct 6, 2007
  5. Emmanouil Angelakis

    Calling external program from within python

    Emmanouil Angelakis, Jul 25, 2008, in forum: Python
    Replies:
    10
    Views:
    582
    Terry Reedy
    Jul 25, 2008
Loading...

Share This Page