简体   繁体   English

Java 运行终端命令并发送输入

[英]Java run terminal command and send input

I have master password for terminal setup and I would like to create a Java program which adds a few extra features.我有终端设置的主密码,我想创建一个 Java 程序,它增加了一些额外的功能。 To do this I need to send and receive input/output from the terminal.为此,我需要从终端发送和接收输入/输出。 I tried what was suggested in Java Program that runs commands with Linux Terminal but didn't have any luck.我尝试了Java 程序中的建议,该程序使用 Linux 终端运行命令,但没有任何运气。 For some reason the input isn't passed in and Your master password: is printed out if I force stop (which is were the input was supposed to be passed in).出于某种原因,输入未传入,如果我强制停止,则会打印出Your master password: (这是应该传入的输入)。 Below is my code, can anyone see what I am doing wrong?下面是我的代码,谁能看到我做错了什么?

try
{
    // Send the command
    Process process = new ProcessBuilder("mpw", "-u", "Name", "-t", "l", "Website").start();
    String key = "somekey";
    OutputStream stdOutput = process.getOutputStream();
    // Send an input
    stdOutput.write(key.getBytes());
    // Store the input (and error) in a buffer
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

    // Read the output from the command:
    int data;
    while ((data = stdInput.read()) != -1)
        System.out.write(data);

    while ((data = stdError.read()) != -1)
        System.out.write(data);

    System.out.flush();
}
catch (IOException e) { e.printStackTrace(); }

Thanks in advance提前致谢

Thanks to Abra I was able to find the solution.感谢 Abra,我能够找到解决方案。 For anyone looking at this later, here is the code that worked:对于稍后查看此内容的任何人,以下是有效的代码:

// Create a new process and run the command
String[] command = new String[] {"mpw", "-u", "Name", "-t", "l", "Website"}; // Can also directly be put into the process builder as an argument without it being in an array
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();

OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();

// Store the input and output streams in a buffer
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
BufferedReader error = new BufferedReader(new InputStreamReader(stderr));

// Send input
writer.write("password\n"); // Don't forget the '\n' here, otherwise it'll continue to wait for input
writer.flush();
//writer.close(); // Add if doesn't work without it

// Display the output
String line;
while ((line = reader.readLine()) != null) System.out.println(line);
// Display any errors
while ((line = error.readLine()) != null) System.out.println(line);

This should work for any command, I got the solution from Writing to InputStream of a Java Process这应该适用于任何命令,我从写入 Java 进程的 InputStream 中得到了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM