Apache Commons Exec 为包含空格的参数生成太多引号?

发布于 2024-10-16 02:14:58 字数 1826 浏览 4 评论 0原文

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 的所有计算机上都可用。我的观点是,错误的运行时参数被传递给外部进程,其中包含转义的引号,这是不应该的。

编辑:我在 Jira

Either there is a bug in Apache Commons Exec, or I am using the API wrong, 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:

@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖不如嘢 2024-10-23 02:14:58

Apache common exec 有带有 handleQuoting 标志的 addArgument 方法。
如果它已打开,则会将参数括在引号中。

默认情况下它是打开的

public CommandLine addArgument(final String argument, boolean handleQuoting) 

Apache common exec has addArgument method with handleQuoting flag.
If it's turned on, then it wraps arguments in quotation marks.

By default it's turned on

public CommandLine addArgument(final String argument, boolean handleQuoting) 
情话已封尘 2024-10-23 02:14:58

这似乎是 Apache Commons Exec 中的真正错误,迄今为止尚未修复。

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

八巷 2024-10-23 02:14:58

你可以尝试下一个对我有用的例子:

  CommandLine cmdLine = new CommandLine("mco");
  cmdLine.addArgument("shellcmd" );
  cmdLine.addArguments( puppetAgents.split(" ") );
  cmdLine.addArgument("-t");
  cmdLine.addArgument(timeout);
  // When writing a command with space use double "
  cmdLine.addArgument("\"\"puppet agent -t\"\"",false);

you can try the next example it works for me :

  CommandLine cmdLine = new CommandLine("mco");
  cmdLine.addArgument("shellcmd" );
  cmdLine.addArguments( puppetAgents.split(" ") );
  cmdLine.addArgument("-t");
  cmdLine.addArgument(timeout);
  // When writing a command with space use double "
  cmdLine.addArgument("\"\"puppet agent -t\"\"",false);
提笔书几行 2024-10-23 02:14:58

你想执行什么课程?您没有准确指定您希望 Java 执行的内容。这需要成为你的第二个论点。

换句话说,如果您尝试运行 com.mycompany.MyExecutable,您的代码应如下所示:

CommandLine cmdLine = new CommandLine("java com.mycompany.MyExecutable");

您可以在创建 CommandLine 对象后附加参数。

What class are you trying to execute? You're not specifying exactly what you want Java to execute. That needs to be your second argument.

In other words, if you're trying to run com.mycompany.MyExecutable, your code should look like this:

CommandLine cmdLine = new CommandLine("java com.mycompany.MyExecutable");

You can either append arguments after the CommandLine object is created.

‖放下 2024-10-23 02:14:58

教程中那样尝试一下怎么样,看看是否有区别?

...
String line = "java what version";
CommandLine cmdLine = CommandLine.parse(line);
executor.execute(cmdLine, resultHandler);
...

How about trying it this way as in the tutorial, to see if that makes a difference?

...
String line = "java what version";
CommandLine cmdLine = CommandLine.parse(line);
executor.execute(cmdLine, resultHandler);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文