JDK-6276512 : (process) Runtime.exec(cmd[]) ignores quotes around arguments (win)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2005-05-26
  • Updated: 2013-07-26
  • Resolved: 2013-07-15
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :

Passing arguments to a process via Runtime.exec(cmd[]), any arguments with quotes (") around them lose the quotes if the String starts with a whitespace.  See the steps to reproduce.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

The following simple classes reproduce:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RunMe
{
   public static void main(String[] args)
      throws Exception
   {
      String[] commandArray =
         {
            "java",
            "-classpath",
            ".",
            "PrintArgs",
            " \"arg 1\"",
         };
      Process p = Runtime.getRuntime().exec(commandArray);
             
      StringBuffer stdout = new StringBuffer();
      
      InputStreamReader isr = new InputStreamReader(p.getInputStream());
      BufferedReader br = new BufferedReader(isr);
      String line = null;
      
      while ((line = br.readLine()) != null)
      {
         stdout.append(line+"\n");
      }
      
      System.out.println(stdout);
   }
}
//------------------------------
public class PrintArgs
{
	public static void main(String[] args)
	{
		for (int i = 0; i < args.length; i++)
		{
			System.out.println("args["+i+"] = [" + args[i] + "]");
		}
	}
}

Observe the the last parameter in commandArray is surrounded by quotes and has a space in front of it.

The output of running RunMe is:
args[0] = [ arg]
args[1] = [1]

The quotes are getting lost.  Removing the space in front of arg1 changes the output to this:
args[0] = [arg 1]





REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Make sure you don't put spaces in front of your arguments :-)
###@###.### 2005-05-26 13:37:40 GMT

Comments
In accordance with MS documentation it is not an issue. Read more here: http://msdn.microsoft.com/en-us/library/17w5ykft.aspx To achieve the desired behavior symbol ["] need to be escaped: - " \"arg 1\"" +" \\\"arg 1\\\""
15-07-2013

Test case for a bug.
15-07-2013

EVALUATION The argument passing/conversion stuff on Windows is really tricky. I'm not sure either the existing or proposed behavior is correct. Shouldn't the app be printing args[0] = [ "arg 1"] The actual rules for Windows command line quoting are surprisingly arcane. See 6214916: -version:<id> argument affects application arguments containing spaces We may not be able to change the behavior here to the "proper" one, since compatibility usually trumps other considerations.
03-08-2005