gpt4 book ai didi

Java执行路径名中带空格的命令

转载 作者:太空狗 更新时间:2023-10-29 22:38:09 29 4
gpt4 key购买 nike

如何执行路径名中有空格的 Java 系统(shell)命令?

我试过添加引号和反斜杠 (),但它不起作用。

ln -s "dir1/dir2" "my\ dir/dir2"

最佳答案

到目前为止,最可靠的方法是使用 Runtime.exec(String[] cmdarray) .

如果您使用 Runtime.exec(String command) , Java 仅在空格处拆分命令。

the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

另见 g++: File not found

或者使用ProcessBuilder像这样:

ProcessBuilder pb = new ProcessBuilder("ln", "-s", "dir1/dir2", "my dir/dir2");
Process p = pb.start();

关于Java执行路径名中带空格的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4916918/

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