Flakey Results from Runtime.exec() On Windows 2000

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    Flakey Results from Runtime.exec() On Windows 2000

    I've been using Runtime.exec() like this:

    Runtime rt = Runtime.getRunt ime();
    try {Process p = rt.exec("MyComm and.bat");} catch (Exception e) {do stuff}

    When I start my Java classes, I start them with a batch file that changes to
    my apps home directory. I've tried exec() with a full pathname (which
    seems to have problems on Windows if it has spaces in it, but I'm not sure
    if that's really the problem), with just the simple short batch file name,
    and with ".\MyCommand.ba t".

    I have one class that is VERY simple that has managed to do this okay, but
    I've tried this in a number of variations. I even took the simple class
    and changed ONLY the batch file name and classname so it would run another
    batch file in the same dir. It wouldn't. (Yes, the file had read and exec
    permissions set.)

    All of these commands worked perfectly on Linux (and I am definately
    changing the pathnames to use the correct file separator). So I have:

    1) exec(command) that works on Linux, but not on Windows, even with
    pathnames corrected,
    2) A class that runs a batch file, but will not run others in the same
    directory, even if they have the correct permission bits set.

    What kind of factors can effect this? I wouldn't think the length of the
    filename should matter. I can't figure out what else would be the
    difference.

    I can post the code of all examples, but it's basically the same as above.
    (I've used a String I defined earlier in some cases and also used a string
    with quotation marks at the start and end, also.) I just can't figure out
    why it works sometimes and not others.

    Any ideas?

    Thanks!

    Hal
  • Hal Vaughan

    #2
    Re: Flakey Results from Runtime.exec() On Windows 2000

    Additional info at bottom:

    Hal Vaughan wrote:
    [color=blue]
    > I've been using Runtime.exec() like this:
    >
    > Runtime rt = Runtime.getRunt ime();
    > try {Process p = rt.exec("MyComm and.bat");} catch (Exception e) {do stuff}
    >
    > When I start my Java classes, I start them with a batch file that changes
    > to
    > my apps home directory. I've tried exec() with a full pathname (which
    > seems to have problems on Windows if it has spaces in it, but I'm not sure
    > if that's really the problem), with just the simple short batch file name,
    > and with ".\MyCommand.ba t".
    >
    > I have one class that is VERY simple that has managed to do this okay, but
    > I've tried this in a number of variations. I even took the simple class
    > and changed ONLY the batch file name and classname so it would run another
    > batch file in the same dir. It wouldn't. (Yes, the file had read and
    > exec permissions set.)
    >
    > All of these commands worked perfectly on Linux (and I am definately
    > changing the pathnames to use the correct file separator). So I have:
    >
    > 1) exec(command) that works on Linux, but not on Windows, even with
    > pathnames corrected,
    > 2) A class that runs a batch file, but will not run others in the same
    > directory, even if they have the correct permission bits set.
    >
    > What kind of factors can effect this? I wouldn't think the length of the
    > filename should matter. I can't figure out what else would be the
    > difference.
    >
    > I can post the code of all examples, but it's basically the same as above.
    > (I've used a String I defined earlier in some cases and also used a string
    > with quotation marks at the start and end, also.) I just can't figure out
    > why it works sometimes and not others.
    >
    > Any ideas?
    >
    > Thanks!
    >
    > Hal[/color]

    I tried reading the error stream from the process I created -- and got
    nothing. So I changed that to the intput stream (which confused me at
    first, because I thought I should be reading the output stream). I finally
    got it working with the code below. Basically, I have an endless loop in
    the thread that reads the output from the process. I added in a 1/100 of a
    second delay so it didn't freeze up the system. This leaves a few
    questions:

    1) How can I tell when this is done and kill the thread? I know I can use
    Process.waitFor (), but that locks up, instead of checking if it is done.
    (The best I can think of is passing the process to yet another thread that
    does nothing but Process.waitFor (); and, after that, Process.destroy ();)
    2) Is there any way to redirect the output of a Windows program without
    sending it to a file so I don't have to read the output to make sure it
    runs?

    Thanks for any thoughts.

    Hal

    Thread Runner = new Thread() {
    public void run() {
    byte[] bOut;
    int iLen;
    Runtime rt = Runtime.getRunt ime();
    Process p = null;
    String sLine = "MyProgram.bat\ n";
    try {
    p = rt.exec(sLine);
    InputStream is = p.getInputStrea m();
    while (true) {
    iLen = is.available();
    bOut = new byte[iLen];
    is.read(bOut);
    try {Thread.sleep(1 0);} catch (Exception e) {}
    }
    } catch (Exception e)
    {System.out.pri ntln("Cannot Run Command: " + sLine + ", Error: " + e);}
    }
    };
    Runner.start();

    Comment

    Working...