windows 转义文件路径中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7596235/
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
Escape whitespace in filepath
提问by tk2000
I am writing a small Java Application and I am having problems with a filepath.
我正在编写一个小型 Java 应用程序,但我遇到了文件路径问题。
I want to execute a batch file with
我想执行一个批处理文件
Runtime.getRuntime().exec("cmd /c start c:\program files\folder\file.bat");
Runtime.getRuntime().exec("cmd /c start c:\program files\folder\file.bat");
But now Java cries because of the whitespace in the filepath.
但是现在 Java 因为文件路径中的空格而哭了。
How can I escape it?
我怎样才能逃脱它?
EDIT:
编辑:
Ok, thank you for the answers guys. But I just ran into a new problem:
好的,谢谢各位大侠的解答。但是我遇到了一个新问题:
If I start the .bat this way it just opens a cmd window and nothing happens. But if I move the .bat into c:/folder/ without spaces it works...the .bat itself is fine too.
如果我以这种方式启动 .bat,它只会打开一个 cmd 窗口,什么也没有发生。但是如果我将 .bat 移动到 c:/folder/ 没有空格它就可以工作…….bat 本身也很好。
回答by Matthew Farwell
You can avoid any problems like this by using the Runtime#execthat takes a String[]:
您可以通过使用采用 String[]的Runtime#exec来避免任何此类问题:
Runtime.getRuntime().exec(new String[] {"cmd", "/c", "start", "c:\program files\folder\file.bat"});
That way you don't have to worry about quoting the file names. However, you still have to worry about quoting \ in the file names.
这样您就不必担心引用文件名。但是,您仍然需要担心在文件名中引用 \。
回答by LordDoskias
Instead of using Runtime.getRuntime it is better to use ProcessBuilder
而不是使用 Runtime.getRuntime 最好使用ProcessBuilder
That way you can have something like:
这样你就可以有类似的东西:
List<String> command = new ArrayList<String>();
command.add("first-arg");
command.add("second arg with spaces") //this one will be accepted as a single argument, eventhough it has spaces
回答by gopeca
A solution for windows 7 at least:
至少适用于 Windows 7 的解决方案:
Runtime.getRuntime().exec("my_program_name.exe \"" + namefile+"\");
The idea is to put namefile between the character ".
这个想法是将名称文件放在字符“.
回答by Ramzi Khahil
you have to escape the slashes so \\
and in windows white space are escaped with %20
. I don't know about other system, could work. But first thing do the \\
.
你必须逃避斜线,所以\\
在 Windows 中,空白被转义为%20
. 我不知道其他系统,可以工作。但首先要做的是\\
.
回答by Dave Newton
I think it cries because of the unescaped back slashes in your string. Quote the file name.
我认为它会因为字符串中未转义的反斜杠而哭泣。引用文件名。
回答by jeha
Put quotes around and quote backslashes:
加上引号并引用反斜杠:
Runtime.getRuntime().exec("cmd /c start \"c:\program files\folder\file.bat\"");
回答by ratchet freak
surround the filepath with "
用包围文件路径 "
Runtime.getRuntime().exec("cmd /c start \"c:\program files\folder\file.bat\"");
and properly escape the \
in the path as well
并\
在路径中正确逃脱
回答by MeBigFatGuy
Runtime.getRuntime().exec("cmd /c start \"c:/program files/folder/file.bat\"");
should work
应该管用