Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to execute multiple commands from command prompt through Java?

807580May 23 2010 — edited May 23 2010
Hi,

I want to execute many command line commands from my Java program. I have written the below code:
HashSet<String[]> commandSet = new HashSet<String[]>();
commandSet.add(new String[]{"cmd", "/C", command1});
...
...
commandSet.add(new String[]{"cmd", "/C", command100});

Process P;
Runtime rt = Runtime.getRuntime();
for(String[] command : commandSet){
    P = rt.exec(command);
    P.waitFor();
}
The problem is that it seems very inefficient to open a new command prompt for each command. Is it possible to execute all commands in the same command prompt?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 20 2010
Added on May 23 2010
7 comments
462 views