gpt4 book ai didi

java - 从 java 运行 linux 命令时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:17 25 4
gpt4 key购买 nike

我正在尝试使用 Process process =Runtime.getRuntime().exec(command) 从 Java 代码运行以下命令,但出现错误。

命令:repo forall -c 'pwd;git status'

错误:'pwd;git: -c: line 0: unexpected EOF while looking for matching''`我可以从 linux 终端运行这个命令,但是当从 java 运行时,问题出在 pwd;git 之后的空格上。谁能帮我?

最佳答案

这是一个 super 经典的错误,坦率地说,我很惊讶你没有通过四处搜索找到答案。

进程不是命令解释器。

但是,如果您只向它传递一个参数,Runtime.exec() 仍会尝试并作为一个整体运行,在这里您最终会像这样 split :

  • repo
  • forall
  • -c
  • 'pwd;git
  • 状态'

这显然不是你想要的。

使用 ProcessBuilder。我不会为你做这一切,但这里是如何开始:

final Process p = new ProcessBuilder()
.command("repo", "forall", "-c", "pwd; git status")
// etc etc
.start();

Link to the javadoc .

关于java - 从 java 运行 linux 命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710011/

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