Solved

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

Posted on 2010-11-24
11
1,977 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 69

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 69

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
 
LVL 69

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 69

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 69

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 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