JDK-4365120 : Runtime.exec(String) does not parse quotes correctly
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.2,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_nt,windows_2000
  • CPU: generic,x86
  • Submitted: 2000-08-23
  • Updated: 2001-11-13
  • Resolved: 2001-11-13
Related Reports
Duplicate :  
Description

Name: stC104175			Date: 08/23/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)



Runtime.exec(String) and Runtime.exec(String, String[]) do not consider the
significance of single and double quote characters when parsing the command-line
string. Right now they parse "xterm -T 'My title' -e ls" as follows:

xterm
-T
'My
title'
-e
ls

such that each line denotes another element in the array ARGV[]. Instead, the
above String should be parsed as:

xterm
-T
My title
-e
ls

Another thing worth noting is that this parsing behavior should be different
under Windows and Unix-based machines as Unix-based machines accept single and
double quotes equaly and Windows-based machines only accept double-quotes.
If you would like me to provide you with the source-code to correct this bug,
please contact me.
(Review ID: 107860) 
======================================================================

Name: krC82822			Date: 03/14/2001


>java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

exec cannot handle commands with "-" parameters.
Below code executes nbtstat with -n parameter.
Output dataset does not get updated with command output.
Command works under C++ and NT Command Prompt.

button12.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        Runtime r = Runtime.getRuntime();
	  try {
	  	r.exec("c:\\windows\\system32\\nbtstat -n > c:\\temp\\tmp.rpt");
	      }
	  catch (java.io.IOException e)
		{ System.out.println("Got exception on button Nbtstat Names " +
e);
		  e.printStackTrace();
		}
      }
    });



Commands with "/" parameters work fine:
Below code executes mem with /d parameter.
 
button11.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        Runtime r = Runtime.getRuntime();
	  try {
	  	r.exec("c:\\windows\\system32\\mem.exe /d > c:\\temp\\tmp.rpt");
		callWord();
		}
	  catch (java.io.IOException e)
		{ System.out.println("Got exception on button Memory Debug " +
e);
		  e.printStackTrace();
		}
      }
    });


Workaround is to place command in a script file:

 button12.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        Runtime r = Runtime.getRuntime();
	  try {
	  	r.exec("c:\\joe\\java\\bin\\nbtstat_n.bat");
	  //	r.exec("c:\\windows\\system32\\nbtstat -n > c:\\temp\\tmp.rpt");
		callWord();
		}
	  catch (java.io.IOException e)
		{ System.out.println("Got exception on button Nbtstat Names " +
e);
		  e.printStackTrace();
		}
      }
    });



where nbtstat_n.bat contains:

c:\\windows\\system32\\nbtstat -n  > c:\\temp\\tmp.rpt
(Review ID: 118798)
======================================================================

Comments
WORK AROUND Name: stC104175 Date: 08/23/2000 I wrote up a OS-dependant PERL script to intercept the outgoing Exec() call, reparse the command-line string, and regenerate the Exec() call.. But this gets ugly and cumbersome. Not the mention the fact it isn't platform independant and requires PERL to be installed on the host machine. ====================================================================== Name: krC82822 Date: 03/14/2001 Workaround is to place command in a script file: button12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { Runtime r = Runtime.getRuntime(); try { r.exec("c:\\joe\\java\\bin\\nbtstat_n.bat"); // r.exec("c:\\windows\\system32\\nbtstat -n > c:\\temp\\tmp.rpt"); callWord(); } catch (java.io.IOException e) { System.out.println("Got exception on button Nbtstat Names " + e); e.printStackTrace(); } } }); where nbtstat_n.bat contains: c:\\windows\\system32\\nbtstat -n > c:\\temp\\tmp.rpt (Review ID: 118798) ======================================================================
11-06-2004

EVALUATION There is no way we can fix it in current API, since the spec for Runtime.exec(String) says that the String is parsed by the default version of StringTokenizer. This fundamental flaw, as well as other flaws of Process should be addressed in 4109888. I am closing this as a duplicate of 4109888. ###@###.### 2001-11-13
13-11-2001