1

I want to run a batch file through java program. The batch file itself runs a exe file with some filename as arguments. I tried this by creating a C program and running that exe through java. Is there any other way to run a batch file which itself runs a exe through java. Thanks in advance...

Suraj Air
  • 2,063
  • 4
  • 22
  • 33
  • possible duplicate of [How do I run a batch file from my Java Application?](http://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application) – Grodriguez Nov 10 '10 at 06:33

4 Answers4

2

You could use Runtime.exec and pass it cmd /c /path/to/your/batch/script.

As of Java 1.5, you can also use ProcessBuilder.

 Process p = new ProcessBuilder("cmd", "/c", "/path/to/batch/file").start();

The API docs for ProcessBuilder details a more complex setup with working directories and such.

wkl
  • 77,184
  • 16
  • 165
  • 176
1

To start with playing with the batch file you have to take some time to learn PROCESSBUILDER and Runtime classes.

Program:

class RunBatch
{
    public static void main(String[] arg){

        Runtime runtime = null;
        try{
            runtime.getRuntime.exec("CMD START /C D:/myBatchFile.bat");   
        }
        catch(RuntimeException e){ 
            e.printStackTrace();
        }
    }
}
Tisho
  • 8,320
  • 6
  • 44
  • 52
Anil Kumar
  • 11
  • 1
1
public class CallingBatch {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("cmd start /c C:/batfile.bat");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("FINISHED");
}
}

Hope this will help you.

Sujeet
  • 104
  • 1
  • 2
  • 10
0

My preferred method of starting any process from within java is to use ProcessBuilder

drekka
  • 20,957
  • 14
  • 79
  • 135