gpt4 book ai didi

java - 可执行文件的 Java 文件路径中的空格

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:56 25 4
gpt4 key购买 nike

好吧,我知道这可能是一个菜鸟问题,但我是 Java 的新手,它可能很容易回答。我想做的是制作一个使用文件路径打开 Firefox 的程序,但文件路径似乎有问题。我做了一些研究并使用双斜杠来取消转义字符,但它仍然不起作用。我认为这与某些目录名称中存在空格这一事实有关。这是我的代码:

import java.io.IOException;

public class Automation {

public static void main(String[] args) throws IOException {
Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
}
}

我知道这很简单,但我还是想不通。感谢您的帮助。

最佳答案

Process p = Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\Mozilla firefox\\firefox.exe\"");

... 或针对 Windows 使用 Java 7 ...

String[] command = new String[] {
"C:" +
File.separator +
"Program Files (x86)" +
File.separator +
"Mozilla firefox" +
File.separator +
"firefox.exe"
};
Process p = Runtime.getRuntime().exec(command);

关于java - 可执行文件的 Java 文件路径中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com