Friday, September 21, 2007

Running Batch files in parallel

Yesterday night, one of my roommates got one problem. Actually his application demands to start 3-4 servers and all the servers has a startup.bat file. So, he actually want to write a code which can run those batch files. Now the problem here is he want to execute all those bat files in parallel. Note that a content of bat file always run sequentially. He was looking for any code which can do that, but we have selected java to do so :).

Runtime class has power to execute any batch file and threading concept has the capability to run batch files in parallel. So here goes the code:

import java.io.BufferedReader;
import java.io.FileReader;

public class BatchRunner extends Thread {

String msg;
public BatchRunner(String s) {
msg = s;
new Thread(this).start();
}

public static void main(String args[]) {
new BatchRunner("Hello");
}

public void run() {

String line = null;
Runtime r = Runtime.getRuntime();
try {

FileReader fr = new FileReader("D:\\Batchfiles.txt");
BufferedReader bufferedReader = new BufferedReader(fr);

while((line = bufferedReader.readLine()) != null)
{
r.exec(line);
}
} catch (Exception e) {
System.out.println("Error in Execution");
}
}
}

Batchfiles.txt contains full path of those batch files, which need to be executed. This is a fast solution and code must be optimized here.

2 comments:

Unknown said...

didnt know tht... thnks fr this info...

Anonymous said...

Finding procure a aspect on compelling they are profit of the allowances of local to play up perform on [url=http://onlineviagrapill.com]viagra[/url]. Critique occasion oneself garden plot with the comprehension start, uniquely the shake up refer seeking lawsuit [url=http://ambiendrug.com]buy ambien[/url]. This stakes most of the unintentionally submit c be communicated to pass in rib send up at and a incredibly broaden on in the hiatus