包含多个空格的参数上的Runtime.exec [英] Runtime.exec on argument containing multiple spaces

查看:131
本文介绍了包含多个空格的参数上的Runtime.exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以进行以下运行吗?

Can anyone make the following run?

public class ExecTest {
  public static void main(String[] args) {
    try {
      //Notice the multiple spaces in the argument
      String[] cmd = {"explorer.exe", "/select,\"C:\\New      Folder\\file.txt\""};

      //btw this works
      //String cmd = "explorer.exe /select,\"C:\\New Folder\\file.txt\"";

      //and surprisingly this doesn't work
      //String[] cmd = {"explorer.exe", "/select,\"C:\\New Folder\\file.txt\""};

      //Update: and (as crazy as it seems) the following also worked
      //String[] cmd = {"explorer.exe", "/select,\"C:\\New", "Folder\\file.txt\""};

      Runtime.getRuntime().exec(cmd);
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

使用Java 6.测试下Vista x64。顺便说一下,获取执行的字符串(你必须使用exec的String版本来获取它)并在Vista的开始菜单的 Search 字段中使用它将按预期运行。 br>
任何帮助将不胜感激。我疯了......!

Using Java 6. Tested under Vista x64. By the way, taking the string that gets executed (you 'll have to use the String version of exec to get it) and using it in the Search field of Vista's start menu will run as expected.
Any help will be greatly appreciated. I'm going nuts..!

更新:
我为我的帖子指出的第二个奇怪的东西添加了一个解决方案,即exec的2个版本表现不同。解决方案基于prunge的答案。 Thnx再次。

Update: I added a solution for the 2nd weird thing my post is pointing out, that the 2 versions of exec behave differently. The solution is based on the answer by prunge. Thnx again.

推荐答案

好的,这不仅仅是一个更新,而且也是一个答案,所以我把它归为一个。根据我能找到的所有信息,理论上应该如下:

Ok, this is not simply an update but also an answer so I'm filing it as one. According to all information I could find, the following should theoretically do it:


String [] cmd = {explorer.exe, / select,\C:\ New,,,,,,,Folder\file.txt \};

String[] cmd = {"explorer.exe", "/select,\"C:\New", "", "", "", "", "", "", "Folder\file.txt\""};

多个空格已被分解为空字符串,并使用了exec的数组版本。
使用上面的数组,我在java.lang.ProcessImpl的第50-75行调试了循环,最后构造了一个字符串。结果字符串是:

The multiple spaces have been broken into empty strings and the array version of exec is used. Using the above array, I debugged the loop in lines 50-75 of java.lang.ProcessImpl where a string is finally constructed. The resulting string was:

explorer.exe / select,C:\ New;       Folder\file。 txt

explorer.exe /select,"C:\New       Folder\file.txt"

这是作为第一个参数传递给ProcessImpl的本地创建方法(第118行同一个类) ,因为它似乎无法正常运行此命令

This is what is passed as the 1st argument to ProcessImpl's native create method (line 118 same class), which as it seems fails to run properly this command.

所以我想这一切都在这里结束......很遗憾。

So I guess it all ends here... sadly.

Thnx prunge指出java bug。
Thnx每个人都有他们的时间和兴趣!

Thnx prunge for pointing out the java bug. Thnx everyone for their time and interest!

这篇关于包含多个空格的参数上的Runtime.exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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