I execute a command from a java program like
Process myProcess = Runtime.getRuntime().exec("sudo cat /etc/sudoers"); //It asks for password so I send password through outputstream from my program.
InputStream inputStream = myProcess.getInputStream();
OutputStream outputStream = myProcess.getOutputStream();
outputStream.write("mypassword".getBytes()); // write password in stream
outputStream.flush();
outputStream.close();
But the problem is that it again asks me for the password, since I already send the password through the output stream from my program. To solve this, I tried so many times, but did not.
Using a shell script, I can provide a password for the terminal, and my program works fine but this is not the most flexible way.
Can you suggest me a way to provide a password through my java program? (instead of shell programming)
source
share