runtime.exec problems

Discussion in 'Java' started by Memo, Jun 25, 2011.

  1. Memo

    Memo Guest

    I can't get runtime.exec to execute a program in Java. I am trying to
    execute a bat file. I read that in order to run bat files as opposed
    to .exe files the runtime command has to include "cmd /c start" before
    the name of the bat file. I have run bat files successfully this way
    before.

    The command that I am running works when typed from the command line.
    It is:

    z:/n4/pkg/MrServers/MrVista/Simu/StartSimEnv.bat -AutoStart
    GOLD_256x512.dat

    When I try the following code:

    File file = new File(imageDir);
    cmd = "C:\WINDOWS\system32\cmd /c start "+simDir+"StartSimEnv.bat -
    AutoStart " + imageName;

    IJ.log("cmd = " + cmd);

    Runtime runtime = Runtime.getRuntime();

    try{
    Process proc = runtime.exec(cmd,null,file);
    }
    catch(Exception e)
    {
    IJ.log(e.toString());
    }

    StartSimEnv.bat file gives a message about the usage being wrong as if
    I'm giving the wrong parameters. If I delete imageName from the cmd
    string it doesn't complain about usage but doesn't do the task I want
    it to do.

    I tried putting the parameters in an array as follows:


    File file = new File(imageDir);
    String[] cmdArray = {cmdPath, "/c", "start",simDir+"StartSimEnv.bat","-
    AutoStart",imageName};

    IJ.log("cmdArray = " + cmdArray[0] + " " + cmdArray[1] + " " +
    cmdArray[2] + " " + cmdArray[3] + " " + cmdArray[4] + " " +
    cmdArray[5]);
    Runtime runtime = Runtime.getRuntime();

    try{
    Process proc = runtime.exec(cmdArray,null,file);
    }
    catch(Exception e)
    {
    IJ.log(e.toString());
    }

    I still get a message about usage.
    I have made efforts with process builder also but can't get that to
    work either. Does anyone have any ideas why this isn't working?
    Memo, Jun 25, 2011
    #1
    1. Advertising

  2. Memo

    Eric Sosman Guest

    On 6/25/2011 1:58 PM, Memo wrote:
    > I can't get runtime.exec to execute a program in Java. I am trying to
    > execute a bat file. I read that in order to run bat files as opposed
    > to .exe files the runtime command has to include "cmd /c start" before
    > the name of the bat file. I have run bat files successfully this way
    > before.
    >
    > The command that I am running works when typed from the command line.
    > It is:
    >
    > z:/n4/pkg/MrServers/MrVista/Simu/StartSimEnv.bat -AutoStart
    > GOLD_256x512.dat
    >
    > When I try the following code:
    >
    > File file = new File(imageDir);
    > cmd = "C:\WINDOWS\system32\cmd /c start "+simDir+"StartSimEnv.bat -
    > AutoStart " + imageName;


    I assume `cmd' is a String, and `simDir' and `imageName' are things
    that have reasonable toString() methods. Even so, I cannot imagine how
    this would get through a Java compiler, since the only context I can
    think of that would make it valid would be to surround it with /* */.

    Here's a thought: Show us the actual code, not a half-baked
    uncompilable paraphrase. I could make a bunch of guesses about what
    your code actually is, and then I could try to debug the code I'd
    guessed at -- but the thing you want debugged is your code, not my
    guesswork.

    --
    Eric Sosman
    d
    Eric Sosman, Jun 25, 2011
    #2
    1. Advertising

  3. Memo

    Roedy Green Guest

    On Sat, 25 Jun 2011 10:58:27 -0700 (PDT), Memo <>
    wrote, quoted or indirectly quoted someone who said :

    >I can't get runtime.exec to execute a program in Java. I am trying to
    >execute a bat file. I read that in order to run bat files as opposed
    >to .exe files the runtime command has to include "cmd /c start" before
    >the name of the bat file. I have run bat files successfully this way
    >before.


    see http://mindprod.com/jgloss/exec.html
    for the standard gotchas.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One of the great annoyances in programming derives from the irregularity
    of English spelling especially when you have international teams.
    I want to find a method or variable, but I don't know precisely
    how its is spelled or worded. English is only approximately phonetic.
    Letters are randomly doubled. The dictionary often lists variant spellings.
    British, Canadian and American spellings differ.I would like to see an
    experiment where variable names were spelled in a simplified English, where
    there were no double letters.I also think you could add a number of rules
    about composing variable names so that a variable name for something would
    be highly predictable. You would also need automated enforcement of the
    rules as well as possible.
    Roedy Green, Jun 25, 2011
    #3
    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. Bob Connell
    Replies:
    4
    Views:
    1,315
  2. Manish Hatwalne

    Problems with Runtime.exec()

    Manish Hatwalne, Jul 21, 2004, in forum: Java
    Replies:
    2
    Views:
    5,662
    Manish Hatwalne
    Jul 22, 2004
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,073
    Gordon Beaton
    May 22, 2006
  4. tedsuzman
    Replies:
    2
    Views:
    7,051
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  5. Ted
    Replies:
    1
    Views:
    443
    Duncan Booth
    Jul 22, 2004
Loading...

Share This Page