commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "DHadka (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (EXEC-54) Problem with argument containing spaces
Date Thu, 21 Feb 2013 20:06:14 GMT

    [ https://issues.apache.org/jira/browse/EXEC-54?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13583499#comment-13583499
] 

DHadka edited comment on EXEC-54 at 2/21/13 8:05 PM:
-----------------------------------------------------

I'm running into a similar issue as above.  In my case, I want to pass a folder on the command
line, such as {{C:\Documents and Settings\user\folder\}}.  So I call {{command.addArgument("C:\\Documents
and Settings\\user\\folder\\")}}.  Commons Exec then adds the quotes around the argument.
 But on Windows, the trailing \ in the path escapes the quote, which then is passed as part
of the argument.  In this case, the program receives the argument as: {{C:\Documents and Settings\users\folder"}},
with the quote as part of the argument.  This creates a subtle and hard to track error condition.

Consider these cases:
C:\NoSpace\in\path - Works correctly
C:\NoSpace\in\path\ - Works correctly (no quotes are added since no spaces in path)
C:/NoSpace/in/path - Works correctly
C:/NoSpace/in/path/ - Works correctly
C:\With Space\in\path - Works correctly
C:\With Space\in\path\ - Fails due to this bug
C:/With Space/in/path - Works correctly
C:/With Space/in/path/ - Works correctly
                
      was (Author: dhadka):
    I'm running into a similar issue as above.  In my case, I want to pass a folder on the
command line, such as C:\Documents and Settings\user\folder\.  So I call command.addArgument("C:\\Documents
and Settings\\user\\folder\\").  Commons Exec then adds the quotes around the argument.  But
on Windows, the trailing \ in the path escapes the quote, which then is passed as part of
the argument.  In this case, the program receives the argument as: C:\Documents and Settings\users\folder",
with the quote as part of the argument.  This creates a subtle and hard to track error condition.

Consider these cases:
C:\NoSpace\in\path - Works correctly
C:\NoSpace\in\path\ - Works correctly (no quotes are added since no spaces in path)
C:/NoSpace/in/path - Works correctly
C:/NoSpace/in/path/ - Works correctly
C:\With Space\in\path - Works correctly
C:\With Space\in\path\ - Fails due to this bug
C:/With Space/in/path - Works correctly
C:/With Space/in/path/ - Works correctly
                  
> Problem with argument containing spaces
> ---------------------------------------
>
>                 Key: EXEC-54
>                 URL: https://issues.apache.org/jira/browse/EXEC-54
>             Project: Commons Exec
>          Issue Type: Bug
>    Affects Versions: 1.1
>         Environment: Mac OsX 10.6.6, JVM 1.6.0
>            Reporter: Jeremias Rößler
>            Assignee: Siegfried Goeschl
>              Labels: arguments, quotes, spaces
>
> I am new to Commons Exec, so this could also be an error in usage, but... 
> When I use the {{CommandLine}} class to add a argument that contains spaces, some quotes
are added and are then part of the argument that is given.
> For example: When I call {{java "what version"}} I get {{java.lang.NoClassDefFoundError:
what version}}, and when I call {{java "\"what version\""}} (which contains escaped quotes,
that are part of the command line argument itself), I get {{java.lang.NoClassDefFoundError:
"what version"}}.
> So the following test fails, because as you can see in the last line, Apache Exec is
producing the latter version where it should have produced the first version:
> {code:java}
> 	@Test
> 	public void testArgumentQuoting() throws Exception {
> 		String argument = "what version";
> 		DefaultExecutor executor = new DefaultExecutor();
> 		DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
> 		ByteArrayOutputStream out = new ByteArrayOutputStream();
> 		PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
> 		executor.setStreamHandler(streamHandler);
> 		CommandLine cmdLine = new CommandLine("java");
> 		cmdLine.addArgument(argument);
> 		executor.execute(cmdLine, resultHandler);
> 		resultHandler.waitFor();
> 		String resultPattern = "Exception in thread \"main\" java\\.lang\\.NoClassDefFoundError:
([\\w \"]+)";
> 		Pattern pattern = Pattern.compile(resultPattern);
> 		Matcher matcher = pattern.matcher(out.toString());
> 		Assert.assertTrue(matcher.find());
> 		// Note: Result should be <what version> and NOT <"what version">!
> 		Assert.assertEquals(argument, matcher.group(1));
> 	}
> {code} 
> Note that the same test passes if the space is removed from the argument. Please also
note, that I am not trying to start an external Java process, but this is merely an example
that I assume will work on every developers machine.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message