Link to home
Start Free TrialLog in
Avatar of FerrousHombre
FerrousHombre

asked on

How do I run two consecutive DOS commands through a ProcessBuilder object?

I'm trying to run DOS commands through a ProcessBuilder object in Java, and am able to get a single command to run, but not multiple.  In the code below, I see a return from the processes' output stream for "cmd.exe", but not "dir" or "exit".  Do I need to enter the commands differently?
(I'm on a Windows Vista machine.)

        String[] cmds = {"cmd.exe","dir","exit"};
        ProcessBuilder processBuilder = new ProcessBuilder(cmds);
        processBuilder.start();

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

You want to execute DOS commands, so you need to keep them in cmd.exe:
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe /c dir & exit");
        processBuilder.start();

Open in new window

Though the exit is superfluous here.
You may also want to explore your options with that "&" symbol -  

One ampersand means do this AND this
Two together (&&) mean do thing two only if thing one succeeded (nonzero exit code)
Two Pipes (||) mean do thing two only if thing one failed
you can also use parenthesis to logically group commands.


this page has a good overview of the ways you can use single line branching: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true 

Avatar of FerrousHombre
FerrousHombre

ASKER

I get an IOException:
java.io.IOException: Cannot run program "cmd.exe /c dir": CreateProcess error=2, The system cannot find the file specified

Open in new window

SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm.  It runs now, but I don't get any output.  When I run it for just "cmd.exe", I'm able to print some output from the processes' input stream.  But when I run for "cmd.exe","/c dir", I get no output.  I need that so I can interrogate a process and confirm it's status.
If you run just cmd.exe, you get a visible box you can type commands in?
cmd.exe with /c option will execute a command or commandline, then exit. If you need the box, use /k instead of /c.
No, I don't get a visible box.  I take the Process created by ProcessBuilder.start(), use getInputStream() on the process, and read from that stream using a BufferedReader:
Process proc = processBuilder.start();
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String readLine;
while(((readLine = br.readLine()) != null) && (readLine.trim().length() > 0)) {
    System.out.println(readLine);
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That, and ending each of the strings with a space, got it printing output again:
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe ", "/k dir ");

Open in new window


Now, technically, this statement only executes a single command, "/k dir ".  I still need to run multiple commands, "cmd.exe" just doesn't need to be one of them.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah.  I was able to get multiple commands to run (and return output) by invoking additional calls to ProcessBuilder.start(), so I got what I needed.  Thanks!