0

I need to change the working directory first. Then run my batch file with some parameters located in .txt file. I searched to open a batch file, though it works I am unable to change working directory and then run it and to pass parameters too.

Java Snippet:

final String dosCommand = "cmd /c start cmd.exe /K";
      final String location = "\"C:/Program Files/.../abc.bat";
      final String trail = "d:\\xyz.txt";
      try {
         final Process process = Runtime.getRuntime().exec(
            dosCommand + " " + location + " " + "pro_wait" + " " + trail);
         final InputStream in = process.getInputStream();
         int ch;
         while((ch = in.read()) != -1) {
            System.out.print((char)ch);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }

On command prompt I am executing command as,

D:\>"C:\Program Files\...\abc.bat" pro_wait d:\xyz.txt 

I am unable to execute complete command from JAVA. Please help. Thanks in advance to all!

LeoNad
  • 3
  • 3
  • `...` means nothing in Windows NT. In Win 9x it meant two ancestors away for compatibility with Novell OS. –  Apr 15 '16 at 10:18
  • 1
    `cmd /c "cd DIRECTORY & abc.bat"` will do that. –  Apr 15 '16 at 10:33
  • Have you looked at: http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java – Romain Hippeau Apr 15 '16 at 13:55

1 Answers1

0

You should remove the "start" from the dosCommand and ensure the quotes around the batch file are complete. You can then specify the working directory together with the command in the exec method:

    final String dosCommand = "cmd /c cmd.exe /K";
    final String location = "\"C:/Program Files/.../abc.bat\"";
    final String trail = "d:\\xyz.txt";
    try {
        final Process process = Runtime.getRuntime().exec(
                dosCommand + " " + location + " " + "pro_wait" + " " + trail,
                null, new File("D:\\"));
        final InputStream in = process.getInputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            System.out.print((char) ch);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
Thor Stan
  • 86
  • 3