I have master password for terminal setup and I would like to create a Java program which adds a few extra features. 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. 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). 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
-
Maybe this will help: How to use ProcessBuilder to continually interact with a CLI programAbra– Abra2019年11月14日 10:53:28 +00:00Commented Nov 14, 2019 at 10:53
-
@Abra I tried just copy pasting and running the code and after changing command[0] and command[1] to "/bin/bash" and "-c" respectively, I got it to work for some commands (ls, pwd). But, I had the same issue with the command I want to run. It just seems to get held up waiting for an inputduqhiqiku– duqhiqiku2019年11月14日 19:56:59 +00:00Commented Nov 14, 2019 at 19:56
-
Writing to InputStream of a Java Process. I tried it with the code from the link in my previous comment and it worked for me. Of-course I modified that code since it doesn't handle input to the executed process.Abra– Abra2019年11月15日 09:02:51 +00:00Commented Nov 15, 2019 at 9:02
-
@Abra that worked perfectly, thank youduqhiqiku– duqhiqiku2019年11月15日 21:37:58 +00:00Commented Nov 15, 2019 at 21:37
1 Answer 1
Thanks to Abra I was able to find the solution. 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