【发布时间】:2011-10-04 22:07:07
【问题描述】:
如何进行以下运行?
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 来获取它)并在 Vista 开始菜单的 Search 字段中使用它会按预期运行。
【问题讨论】:
-
第一种情况有错字吗?我怀疑在第二种“btw”情况下,/select 被奇怪地对待,因为它是 arg[0] 的一部分。提供 dir 作为 arg 将打开该文件夹。鉴于它们的名称都相同,您很容易错过它比您预期的低一个。我会将它们重命名为不同的。
-
嗯,第一种和第二种情况是相同的命令。在这里,我只是演示了 exec 的数组版本 fails 在这种情况下,而字符串版本 work。路径就在那里,恐怕无关紧要,我可以使用任何东西.. thnx 回答