10

I am trying to run a batch file that is in another directory from my Java executable. I have the following code :

    try {
        Process p =  Runtime.getRuntime().exec("cmd /c start \"C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert\\upsert.bat\"") ;           
    } catch (IOException ex) {
    }

The result is that the program opens a cmd window in the root directory where the program was run at and doesn't access the file path I provided.

Ricardo
  • 369
  • 3
  • 5
  • 18
  • 1
    If you say "start /?" from a Command Prompt window, it will describe options to the `start` command; there's a "/D " option that might help. – ajb Sep 30 '13 at 20:54

5 Answers5

34

Rather than Runtime.exec(String command), you need to use the exec(String command, String[] envp, File dir) method signature:

Process p =  Runtime.getRuntime().exec("cmd /c upsert.bat", null, new File("C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert"));

But personally, I'd use ProcessBuilder instead, which is a little more verbose but much easier to use and debug than Runtime.exec().

ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "upsert.bat");
File dir = new File("C:/Program Files/salesforce.com/Data Loader/cliq_process/upsert");
pb.directory(dir);
Process p = pb.start();
ungato
  • 153
  • 2
  • 12
rob
  • 6,147
  • 2
  • 37
  • 56
  • 1
    Dir is the same path that I included previously? ("\"C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert\\upsert.bat\"") – Ricardo Sep 30 '13 at 21:00
  • If your using ProcessBuilder, I believe you won't need the quotes – MadProgrammer Sep 30 '13 at 21:00
  • @Ricardo Thanks for the clarification; I wasn't making any assumptions. I'll update the answer since that makes it a little simpler. – rob Sep 30 '13 at 21:03
  • @MadProgrammer yes, I think you're right--I had just copied & pasted the path. – rob Sep 30 '13 at 21:04
  • @Ricardo I've simplified it a little further by using forward slashes instead of less readable double-backslashes in the path. Java's File API recognizes both styles. – rob Sep 30 '13 at 21:08
  • 1
    @rob I recently [rejected an edit](http://stackoverflow.com/review/suggested-edits/9107778) from a user claming the edit was for a compilation error. Do edit your answer if it is valid. – Lucky Aug 10 '15 at 10:55
  • @rob Your proposed solution did the trick , but I also want to open a **black command window** so that I can **_see_** the output of commands which are in the .bat file.Any views on this? – Saurabh Bhoomkar Jun 21 '16 at 04:02
  • 1
    Problem Solved , to maintain the command window :- `List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/C", "Start", "run.bat"});` – Saurabh Bhoomkar Jun 21 '16 at 04:32
  • 2
    @SaurabhBhoomkar sure, simply replace the */c* with */k*. Note that if your process writes a significant amount of information to STDOUT or STDERR, you will need to consume those streams. You can kick off threads that read the process' console streams and write that output to your Java program's console and/or log file. If you do not consume the streams, the process may block and appear to hang when the buffer(s) fill up. – rob Jun 21 '16 at 04:39
6

try following

try {
            String[] command = {"cmd.exe", "/C", "Start", "D:\\test.bat"};
            Process p =  Runtime.getRuntime().exec(command);           
        } catch (IOException ex) {
        }
dev2d
  • 4,245
  • 3
  • 31
  • 54
  • 4
    It's early so I may be missing something, but how is this different from what the OP is doing? They need to execute a batch file in a specific location – MadProgrammer Sep 30 '13 at 21:09
3

Following is worked for me

File dir = new File("E:\\test");
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "Start","test.bat");
        pb.directory(dir);
        Process p = pb.start();
2

Your code is fine, but the problem is inside the batch file.

You have to show the content of the bat file, your problem is in the paths inside the bat file.

kdureidy
  • 960
  • 9
  • 26
0
import java.lang.Runtime;

Process run = Runtime.getRuntime().exec("cmd.exe", "/c", "Start", "path of the bat file");

This will work for you and is easy to use.

Brandon
  • 4,491
  • 6
  • 38
  • 59