Question: I want to execute a `.bat` file from a Java program.

I declared the command for the `.bat` like:

`"cmd.exe", "/C", "Start", "C:\\File\\batfile.bat"`

I use Windows XP and Eclipse Helios.

### Code

```
String cmd;
        try {
            String[] command = { "cmd.exe", "/C", "Start", "C:\\File\\batfile.bat" };
                Runtime r = Runtime.getRuntime();
                Process p = r.exec(command);
                p.waitFor();

                } catch (Exception e) 
                {

                System.out.println("Execution error");} 

```

Why is it looking for my `.bat` file in the **project directory** of Eclipse?

Answer: 
Try this instead:

```
String com = System.getEnv("ComSpec") != null ? System.getEnv("ComSpec") : "C:\\Windows\\System32\\cmd.exe";

String[] command = { com, ...... }

```

ComSpec is often set to the path of cmd.exe. If not, use the full (expected path). You could also look for it in %SystemRoot%\system32. Or even %path%. But just checking ComSpec is better than using cmd.exe with nothing else by default.

As someone else pointed out, your default working directory when running from Eclipse is usually the Eclipse project folder.

It's generally good practice not to rely on the working folder being anything useful. Instead specify paths to anything needed, or search the path (if the app doesn't do so for you).
```
Runtime.getRuntime().exec("cmd /c start C:\\File\\batfile.bat");

```