tags:

views:

4690

answers:

6

Hi, Is there any way to open the command prompt and change directory in the command prompt and run the batch file in the same command prompt using java.

I know how to open the command prompt using java. Thanks,

+1  A: 

You can encode the CD and the batch file in the value for cmd.exe /K. From the doco (cmd /?):

Note that multiple commands separated by the command separator '&&' are accepted for string if surrounded by quotes.

For Example:

cmd /C "CD C:\ && Dir"
cmd /C "CD C:\Windows && Dir"
cmd /C "CD C:\Windows && MySuperSuperBatchFile"

For more detail, run:

cmd /?

from the command line.

Software Monkey
can i have example for that
A: 

It's difficult to do from Java for goofy platform-independence reasons: basically what if you're running java on a system that doesn't have hierarchical directories?

There are a number of workarounds depending on what you're really trying to do, but possibly the simplest is to run the eventual command using java.lang.Runtime.exec().

.

Charlie Martin
A: 

Keep the batch file in "path". You can execute it without going to any specific directory.

(For example you can have an entry "set path=%path%;C:..........\YourBatchFile.bat" in Autoexec.bat in Windows environment)

Murthy
A: 

get the environment var "comspec" then exec %comspec% /c start/d directory /b batchfile.bat

Ron
+1  A: 

A couple of the java.lang.Runtime.exec() variations does have a dir argument, so I assume you are not thinking of that?

You can compile the following C program and execute as a wrapper to start any program in any directory you want. If you use a String array with Runtime.exec you will avoid all issues of command line parsing/portability/proper quoting of the arguments.

I do not have any windows machine to test on here, but if you compile the C program to cdexe.exe you should be able to use it as the following:

public class Main {
        public static void main(String args[]) {
                String[] s = { "c:\\some\\place\\cdexe.exe",
                "c:\\start\\dir", "c:\\my\\batch\\file.bat", "arg1", "..." };
                try {
                        java.lang.Runtime.getRuntime().exec(s);
                } catch (java.io.IOException e) {
                        e.printStackTrace();
                }
        }
}

I guess unistd.h is maybe not available on windows, but just substitute with one containing a execv prototype.

#include <stdio.h>
#include <unistd.h>  // or hard code "int execv(const char *path, char *const argv[]);"

int main(int argc, char *argv[])
{
        if (argc < 3) {
                fprintf(stderr, "Error: Usage: %s <directory> <program> [arguments]\n", argv[0]);
                return 1;
        }
        if (chdir(argv[1]) < 0) {
                perror("Error");
                fprintf(stderr, "chdir(%s) failed\n", argv[1]);
                return 1;
        }
        argv[1] = argv[2];
        execv(argv[1], &argv[2]);      // use execvp if you want PATH to be searched
        perror("Error");
        fprintf(stderr, "execv returned\n");
        return 0;
}
hlovdal
+1 for pointing out that exec has a dir, -1 for C = 0... sorry
Bill K
+2  A: 

Be wary of Java's exec. It can hang if the batch process fills the output buffer, and cause other weird problems.

I suggest you look at apache exec. Specifically for your needs you should note that the Executor interface has a setWorkingDirectory method.

Basic usage:

Executor exec = new DefaultExecutor();
exec.setWorkingDirectory("C:\\My\\Dir\\")
CommandLine cl = new CommandLine("mybatch.bat");
int exitvalue = exec.execute(cl);
itsadok