macos Mac OS 中的 Java 命令 Runtime.getRuntime().exec()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7979234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java command Runtime.getRuntime().exec() in Mac OS
提问by user897013
I′m using Mac OS Lion, with java version 1.6.0_26
我使用的是 Mac OS Lion,Java 版本为 1.6.0_26
I'm making a small app for Mac in Java with a main menu for the user, so he can choose several options.
我正在用 Java 为 Mac 制作一个小应用程序,为用户提供一个主菜单,因此他可以选择几个选项。
One of them is install an app using a .pkg
其中之一是使用 .pkg 安装应用程序
Everything was working fine with these commands:
使用这些命令一切正常:
File instFolder = new File(System.getProperty("user.dir") + "/foldername/appInstaller.pkg");
String s = "open "+ instFolder.toString();
Process p = Runtime.getRuntime().exec(s);
Then I realized that there is a problem when foldername has spaces or if I copy this java file with the needed subfolders to a USB pen drive with "NO NAME" as name (or some name with spaces).
然后我意识到当文件夹名称有空格时或者如果我将这个带有所需子文件夹的 java 文件复制到带有“NO NAME”作为名称(或一些带有空格的名称)的 USB 笔式驱动器时会出现问题。
Because s will become something like:
open /Volumes/NO NAME/foldername/appInstaller.pkg
or
open /Users/user1/Desktop/folder name/appInstaller.pkg
So when you run the p process, the command will finish where the first space appears on the path
open /Volumes/NO
or
open /Users/user1/Desktop/folder
To try to fix this I changed the s definition for something like this:
因为 s 会变成这样:
open /Volumes/NO NAME/foldername/appInstaller.pkg
or
open /Users/user1/Desktop/folder name/appInstaller.pkg
所以当你运行 p 进程时,命令会在第一个空格处结束出现在路径
open /Volumes/NO
或
open /Users/user1/Desktop/folder
为了解决这个问题,我改变了这样的定义:
String s = "open "+ "\"" + instFolder.toString() + "\"";
It stopped working fine. The strange thing is that if i copy the s value (after creating the s variable) and paste it in the terminal it works:
open "/Users/user1/Desktop/folder name/appInstaller.pkg"
but running it from Java it does't work.
它停止正常工作。奇怪的是,如果我复制 s 值(在创建 s 变量之后)并将其粘贴到终端中,它会起作用:
打开“/Users/user1/Desktop/folder name/appInstaller.pkg”
但从 Java 运行它确实如此不行。
Could you help me, please?
请问你能帮帮我吗?
Thanks.
谢谢。
回答by beny23
In order to properly escape arguments, you can use the following:
为了正确转义参数,您可以使用以下内容:
Runtime.getRuntime().exec(new String[] { "open", instFolder.toString() });
Though I would probably to use the more modern ProcessBuilder:
虽然我可能会使用更现代的ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("open", instFolder.toString());
Process p = pb.start();
int exitCode = p.waitFor();
Though thismay be worth a read depending on what you want to do with the processes output.
尽管这可能值得一读,具体取决于您想对进程输出做什么。
Note: edited to reflect question in comment
注意:编辑以反映评论中的问题
回答by Nicolas Modrzyk
it seems your path does not have quotes when turned into the shell.
似乎您的路径在变成 shell 时没有引号。
You should probably add "'" on both sides of your path, so the final shell command will look like:
您可能应该在路径的两侧添加“'”,因此最终的 shell 命令将如下所示:
open 'your path'
instead of
代替
open your path
回答by didinino
Here's a little trick that came out from the answers mentioned above:
这是从上面提到的答案中得出的一个小技巧:
ProcessBuilder pb = new ProcessBuilder(commandString.split(" "));
Say commandString = "killall Mail"
then the split will separate the words making it a String[]
parameter to the ProcessBuilder
.
说commandString = "killall Mail"
那么分裂会分开使其成为一个字String[]
参数的ProcessBuilder
。