我正试图在Java代码中执行Linux命令。它需要更改某些目录的权限。
以下是我的尝试:

String  Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);

当我在终端中使用时,String Cmd中的命令运行得很好。但当我在代码中使用它时,什么也不会发生。没有错误或警告反馈可以帮助我理解我的错误。有什么问题吗?

最佳答案

Java不会神奇地选择bash作为可执行文件。你可能想做些

"bash -c <your command>"

请看这个问题:
How to run unix / shell commands with wildcards using Java?
(而且|也是一个bash东西。Java不会神奇地在进程之间创建管道。)

关于java - Java中的Linux终端命令不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27130282/

10-16 03:49
查看更多