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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16639285/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:32:26  来源:igfitidea点击:

Spaces in Java file path to an executable

java

提问by user2399735

Ok, I know this is probably a noobish question, but I'm pretty new to Java, and it'll probably be fairly easy to answer. What I'm trying to do is make a program that will use a file path to open Firefox, but it seems there is a problem with the file path. I did some research and used the double slash to nullify the escape characters, but it still doesn't work. I think it has to do with the fact that there spaces in some of the directories' names. Here is my code:

好的,我知道这可能是一个菜鸟问题,但我对 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");
    }
}

I know its pretty simple, but I can't still figure it out. Any help is appreciated.

我知道它很简单,但我仍然无法弄清楚。任何帮助表示赞赏。

回答by Mena

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

... or with Java 7 against Windows ...

... 或者使用 Java 7 来对抗 Windows ...

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

回答by Laksitha Ranasingha

The ideal solution for your problem should be like this.

您的问题的理想解决方案应该是这样的。

String [] cmds = new String [1];
cmds[0] = "C:\Program Files (x86)\Mozilla firefox\firefox.exe";
Process p = Runtime.getRuntime().exec(cmds);

This is because Runtime.getRuntime().exec()actually doesn't execute the program as command line interpreter does. So you need to use a parameter array when you have white spaces in the path. you can provide extra flags/options in this array (ex: open).

这是因为Runtime.getRuntime().exec()实际上不像命令行解释器那样执行程序。因此,当路径中有空格时,您需要使用参数数组。您可以在此数组中提供额外的标志/选项(例如:打开)。

This is some extra information. As far as I know Windows is perfectly happy with forward slashes (/), because Windows API accepts forward and backward slashes (starting from MS DOS 2.0i think). for example you can do dir "c:/Program Files (x86)"will work fine give you the directory list. Furthermore, without white spaces Process p = Runtime.getRuntime().exec("C:/SomeProgram/someprogram.exe");works fine . However the recommended way is to get the file separator from the environment. That's using File.separator

这是一些额外的信息。据我所知,Windows 对正斜杠 ( /)非常满意,因为 Windows API 接受正斜杠和反斜杠(从MS DOS 2.0我认为开始)。例如,你可以dir "c:/Program Files (x86)"给你目录列表。此外,没有空格也能Process p = Runtime.getRuntime().exec("C:/SomeProgram/someprogram.exe");正常工作。但是,推荐的方法是从环境中获取文件分隔符。那是用File.separator

回答by xtraclass

"\"C:\ .......\""

So you can "escape" the blanks.

所以你可以“逃避”空白。