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.

1
  • 1
    If you say "start /?" from a Command Prompt window, it will describe options to the start command; there's a "/D <path>" option that might help. Commented Sep 30, 2013 at 20:54

5 Answers 5

35

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();
Sign up to request clarification or add additional context in comments.

9 Comments

Dir is the same path that I included previously? ("\"C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert\\upsert.bat\"")
If your using ProcessBuilder, I believe you won't need the quotes
@rob I recently rejected an edit from a user claming the edit was for a compilation error. Do edit your answer if it is valid.
Problem Solved , to maintain the command window :- List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/C", "Start", "run.bat"});
@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.
|
6

try following

try {
            String[] command = {"cmd.exe", "/C", "Start", "D:\\test.bat"};
            Process p =  Runtime.getRuntime().exec(command);           
        } catch (IOException ex) {
        }

1 Comment

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
4

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();

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.