Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-24
11
Medium Priority
?
2,136 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 71

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 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
 
LVL 71

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 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 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 71

Accepted Solution

by:
Qlemo earned 2000 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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

927 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