所以我试图制作一个通用的应用程序,我试图让它具有打开应用程序的能力.所以,我想要让它打开一个应用程序,如果它是一个单词,但有空格,它永远不会工作.我是这样做的:
if(textField.getText().startsWith("application open")){
try {
String string1 = textField.getText().substring(17, textField.getText().length());
Runtime.getRuntime().exec("open /Applications/\"" + string1 + "\".app");
textArea.append("Opening application " + string1 + "..." + newline);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人能帮助我,那就太好了!霍华德史塔克,提前谢谢
使用的版本exec需要String[]:
这对我有用:
String cmds[] = { "open", "/Applications/Font Book.app" };
Runtime.getRuntime().exec(cmds);
Run Code Online (Sandbox Code Playgroud)
然而,这不是:
Runtime.getRuntime().exec("open /Applications/Font Book.app");
Run Code Online (Sandbox Code Playgroud)
后者只能使用空格来分解命令参数,因此解析为命令open,后跟空格分隔的2个参数.前者 - exec需要a 的版本String[]- 单独传递每个参数,因此没有这个问题.
| 归档时间: |
|
| 查看次数: |
835 次 |
| 最近记录: |