runtime.exec problems


M

Memo

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?
 
Ad

Advertisements

E

Eric Sosman

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.
 
Ad

Advertisements

R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top