Solved

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

Posted on 2010-11-24
11
1,889 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:FerrousHombre
  • 5
  • 5
11 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 34207645
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.
0
 
LVL 7

Expert Comment

by:YarnoSG
ID: 34207740
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

0
 

Author Comment

by:FerrousHombre
ID: 34207780
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

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 34207813
Sorry, my bad. I should have looked at the ProcessBuilder syntax first. You need to provide executable and parameters in different parameters of ProcessBuilder:
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c dir & exit");
processBuilder.start();

Open in new window

0
 

Author Comment

by:FerrousHombre
ID: 34207991
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 68

Expert Comment

by:Qlemo
ID: 34208031
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.
0
 

Author Comment

by:FerrousHombre
ID: 34208102
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

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 34208198
Yes, that would require cmd.exe to run until you kill it. Else it will quit before you can read it's output. Try with the /k.
0
 

Author Comment

by:FerrousHombre
ID: 34208561
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.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 34208693
The issue with multiple comands is that if called separately each command is a single process, requiring its own ProcessBuilder object. If you use cmd.exe as a shell for starting them, they are running in the cmd.exe context, and console output is collected into that single process.
0
 

Author Comment

by:FerrousHombre
ID: 34208905
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!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
So who is this article for? If you are like most of the computer users out there, you probably only realize the meaning of 'System maintenance' after something goes wrong. This article is for you if you care about keeping your system working opti…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now