gpt4 book ai didi

java - 使用 ProcessBuilder 运行 shell 脚本

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:33 24 4
gpt4 key购买 nike

我正在尝试使用 Java 和 ProcessBuilder 运行脚本。当我尝试运行时,收到以下消息:error=2,没有那个文件或目录。

我不知道我做错了什么,但这是我的代码(ps:我试图只执行不带参数的脚本,但错误是一样的:

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};
ProcessBuilder p = new ProcessBuilder(command);

try {

// create a process builder to send a command and a argument
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;

log.info("Output of running " + command + " is: ");
System.out.println("Output of running " + command + " is: ");
while ((line = br.readLine()) != null) {
log.info(line);
}

}

最佳答案

尝试替换

String[] command = {"/teste/teste_back/script.sh, "+argument1+", "+argument+""};

String[] command = {"/teste/teste_back/script.sh", argument1, argument};

引用ProcessBuilder获取更多信息。

ProcessBuilder(String... command)

Constructs a process builder with the specified operating system program and arguments.

关于java - 使用 ProcessBuilder 运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45570827/

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