I am trying to change user password in linux from java program by sending password in outputstream but it is not done.

My java program is like

```
Process process = Runtime.getRuntime().exec("sudo passwd sampleuser");

OutputStream outputStream = process.getOutputStream();

InputStream inputStream = process.getInputStream();

PrintWriter printWriter=new PrintWriter(outputStream);

printWriter.write("123456");

printWriter.write("\n");

printWriter.flush();

```

My program fails here and it ask for password but I does not want this case.

Is there any possibility for providing password from java program ? can you suggest me,how I will do it successfully or is there any shell api's available for it.

Same thing is done successful when I try using shell script and calling it from my java program as

```
Process process = Runtime.getRuntime().exec("bash first.sh");

```

My shell script is

```
i="123456"

echo -e $i"\n"$i|sudo -S passwd sampleuser

```

It changes user password successfully.