我怎样才能进行以下运行?
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 版本来获取它)并在SearchVista 开始菜单的字段将按预期运行。
好吧,这不仅仅是一个更新,也是一个答案,所以我将其作为一个答案归档。根据我能找到的所有信息,理论上应该执行以下操作:
String[] cmd = {"explorer.exe", "/select,\"C:\New", "", "", "", "", "", "", "文件夹\file.txt\" “};
多个空格已被分解为空字符串,并使用 exec 的数组版本。
使用上面的数组,我调试了 java.lang.ProcessImpl 第 50-75 行中的循环,其中最终构造了一个字符串。结果字符串是:
explorer.exe /select,"C:\新建 文件夹\file.txt"
这是作为 ProcessImpl 的第一个参数传递的内容native create
方法(第 118 行同一个类),看起来无法正确运行此命令.
所以我想这一切都结束了......可悲的是。
感谢 prunge 指出 java bug。
感谢大家的时间和兴趣!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)