Tuesday, May 12, 2009

Running .bat file using Java

Hello folks,

I know that some people try to find some information in the blog about .bat execution using Java.

It's perfectly possible. Compile and execute the following code:

public class RunBat {

public static void main(String[] args) throws Exception {
String batFileName = "name.bat"
Process p = Runtime.getRuntime().exec(batFileName);
System.out.println("Waiting .bat file finishes execution");
p.waitFor();
}
}

The String batFileName can assume one of the following values:
1-) .Bat file simple name if the .class of the .java above is in the same directory.
2-) .Bat file simple name if it is recognized by the operating system, e.g: The file can be found by the path.
3-) .Bat absolute name.

No comments:

Post a Comment