0

I execute an external batch file from Java and pass a special attribute to it. I've found that several characters are passed incorrectly to my batch file. I couldn't find any limitations mentioned in ProcessBuilder class nor method of escaping those chars.

public static void main(String[] args) throws IOException {
    /*
     * correct processing: almost everything (even #$*%:'\/~?)
     * fatal error:        |<>
     * trimming:           ;=&
     */
    ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "batch.bat", "a;b;c;d");
    final Process process = builder.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
       System.out.println(line);
    }
    System.out.println("Program terminated!");
}

My batch file is just

echo %1

The code above prints only the first character before the semi-colon (a).

I run this on Win7 and JDK 7 64-bit.

Curently I replacing all these characters with rare ones and the reverse replacement is done later in my final script.

Any idea how to pass these characters correctly without any 'translation'?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jan Tosovsky
  • 498
  • 5
  • 18
  • if you try to execute `batch a;b;c;d` from the command line, it will output `a` too. So the behaviour is consistent with the windows prompt. You might find this other post interesting: http://stackoverflow.com/questions/2477554/processing-semicolon-on-command-line – assylias Jun 26 '13 at 12:05

2 Answers2

2

Microsoft Support Site stated that

When a semicolon (;) or equal sign (=) is used as a command line argument in a batch file, it is treated as a blank space.

cmd /? stated that

The special characters that require quotes are:
     <space>
     &()[]{}^=;!'+,`~

So just use

batch.bat "a;b;c;d"

That is

new ProcessBuilder("cmd", "/c", "batch.bat", "\"a;b;c;d\"");

Later I spotted the output is

"a;b;c;d"
Program terminated!

Where the extra " may not be what you want. Use %~1 to strip it off. That is, change your bat file to:

echo %~1
Community
  • 1
  • 1
johnchen902
  • 9,531
  • 1
  • 27
  • 69
0

Create your ProcessBuilder with the last parameter quoted with ""

ProcessBuilder builder =
               new ProcessBuilder("cmd", "/c", "batch.bat", "\"a;b;c;d\"");

This would take care of all these characters

// fatal error:        |<>
// trimming:           ;=&
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89