gpt4 book ai didi

java - 如何修复 "StreamGobbler"类以便与 bash 应用程序正常工作?

转载 作者:行者123 更新时间:2023-12-01 18:30:46 24 4
gpt4 key购买 nike

我在谷歌上搜索了一个通过 Java 键盘输入来运行应用程序的解决方案,但是当我尝试运行 sudo apt update 时,该程序显示了所有信息(包括密码要求),但它没有显示您想继续吗? [Y/n] 在执行结束时(当我在终端上运行相同的命令时,它会显示确认行)。

StreamGobbler 类

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class StreamGobbler implements Runnable {
private final PrintStream out;
private final Scanner inScanner;
private final String name;

public StreamGobbler(String name, PrintStream out, InputStream inStream) {
this.name = name;
this.out = out;
inScanner = new Scanner(new BufferedInputStream(inStream));
}

@Override
public void run() {}
}

Bash 执行器类

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;

public class BashExecutor {
private static PrintStream out;
private final ArrayList<String> processLinesList = new ArrayList();
private String lastLine = "";

private void GetProcessResponses(final Process process, boolean showMessages, boolean keepAllLog){
this.processLinesList.clear();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
try {
while ((line = input.readLine()) != null) {
if (showMessages) System.out.println(line);
if (keepAllLog) this.processLinesList.add(line);
lastLine = line;
}
} catch (IOException e) {
e.printStackTrace();
}
}

public String GetLastLineExecution(){
return this.lastLine;
}

public int ExecuteBashCommand(String command, boolean isVerbose, boolean keepAllLog){
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", command);
int errorCode = -1;
try {
Process proc = builder.start();
InputStream errStream = proc.getErrorStream();
InputStream inStream = proc.getInputStream();
OutputStream outStream = proc.getOutputStream();


new Thread(new StreamGobbler("in", out, inStream)).start();
new Thread(new StreamGobbler("err", out, errStream)).start();

out = new PrintStream(new BufferedOutputStream(outStream));
GetProcessResponses(proc, isVerbose, keepAllLog);
errorCode = proc.waitFor();
} catch (IOException | InterruptedException e) {
} finally {
if (out != null) {
out.close();
}
}
return errorCode;
}

public static void main(String[] args) {
BashExecutor bashExecutor = new BashExecutor();
boolean isVerbose = true;
boolean keepAllLog = false;
int exitValue = bashExecutor.ExecuteBashCommand("sudo apt upgrade", isVerbose, keepAllLog);
System.out.println("Exit Value: " + exitValue);
}

}

我不知道这些类(class)出了什么问题。

最佳答案

我找到了解决这个问题的方法!

BashExecuter 类上,我刚刚在 ExecuteBashCommand 函数上添加了这些行,位于 builder.command("bash", "-c"行之后,命令);

builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);

因此,函数 ExecuteBashCommand 的最终版本是:

public int ExecuteBashCommand(String command, boolean showMessages, boolean keepAllLog){
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", command);
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
int errorCode = -1;
try {
Process proc = builder.start();
InputStream errStream = proc.getErrorStream();
InputStream inStream = proc.getInputStream();
OutputStream outStream = proc.getOutputStream();

new Thread(new StreamGobbler("in", out, inStream)).start();
new Thread(new StreamGobbler("err", out, errStream)).start();

out = new PrintStream(new BufferedOutputStream(outStream));
GetProcessResponses(proc, showMessages, keepAllLog);

errorCode = proc.waitFor();
}
catch (IOException | InterruptedException e) {
}
finally {
if (out != null) {
out.close();
}
}
return errorCode;
}

关于java - 如何修复 "StreamGobbler"类以便与 bash 应用程序正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60169815/

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