Apache Commons Exec对包含空格的参数产生过多的引号吗? [英] Apache Commons Exec produces too many quotes for arguments containing spaces?

查看:118
本文介绍了Apache Commons Exec对包含空格的参数产生过多的引号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache Commons Exec中有错误,或者我使用的API错误,但是当我使用CommandLine类添加包含空格的参数时,会添加一些引号,然后将其作为该参数的一部分给出了.

例如:当我调用java "what version"时,我会得到java.lang.NoClassDefFoundError: what version,而当我调用java "\"what version\""(其中包含转义引号,这是命令行参数本身的一部分)时,我会得到java.lang.NoClassDefFoundError: "what version".

因此以下测试失败,因为正如您在最后一行中看到的那样,Apache Exec正在生成后一个版本,而在该版本中本应生成第一个版本:

@Test
public void testArgumentQuoting() throws Exception {
    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("what version");
    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("what version", matcher.group(1));
}

现在我想知道:

  • 这是一个错误吗?
  • 如果是这样:是否有一种方法可以解决此问题(一种解决方法)?
  • 如果不是:我在做什么错了?

我正在尝试执行一个进程,我认为很少有人会在他们的计算机上使用.因此,我改用java,因为此命令在人们开发Java的所有计算机上都可用.我的观点是,错误的运行时参数传递给了外部进程,该进程包含转义引号,但不应这样做.

我已将此文件提交给解决方案

这似乎是 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:

@Test
public void testArgumentQuoting() throws Exception {
    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("what version");
    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("what version", matcher.group(1));
}

Now I want to know:

  • Is this a bug?
  • If so: Is there a way to circumvent this problem (a workaround)?
  • If not: What am I doing wrong?

Edit: I am trying to execute a process which I think fewest people will have on their machine. So I am using java instead as this command should be available on all machines where people develop Java. My point is that the wrong runtime argument is passed to the external process, containing escaped quotes, which it shouldn't.

Edit: I made this a filed bug for commons exec at Jira.

解决方案

This seems to be a real bug in Apache Commons Exec, which to date has not been fixed.

这篇关于Apache Commons Exec对包含空格的参数产生过多的引号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆