something like code below is not working:
has anyone here ever executed passwd this way successfully?
String[] callAndArgs = { "passwd",
"ehsan" };
try {
String line = null;
Process child = rt.exec(callAndArgs);
System.out.println("111");
BufferedReader br = new BufferedReader( new InputStreamReader(
child.getInputStream() ) );
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(
child.getOutputStream() ) );
System.out.println(br.readLine() );
bw.write( "password" );
System.out.println(br.readLine() );
bw.write( "password" );
System.out.println( "Data : " + br.readLine() );
System.out.println( "DEBUGGER" );
child.destroy();
br.close();
bw.close();
System.out.println("Process exit code is: " + child.exitValue());
}
catch(IOException e) {
System.err.println(
"IOException starting process!");
}
Post code using the formatting tags...
http://forum.java.sun.com/features.jsp#Formatting
> something like code below is not working:
HOW is it not working?
> has anyone here ever executed passwd this way
> successfully?
Many people have successfully used Runtime.exec() to run console applications.
My problem is in executing passwd, and im asking if anyone has ever written a program in java to change user acounts passwords using passwd command
I cant get the output of "passwd"command and I cant send any streams to the command
String[] callAndArgs = { "passwd",
"poli" };
Process child = rt.exec(callAndArgs);
InputStream stdin = child.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
InputStream stderr = child.getErrorStream();
InputStreamReader isr2 = new InputStreamReader(stderr);
BufferedReader br2 = new BufferedReader(isr2);
while ( (line = br.readLine()) != null) // not working -- program halts here
System.out.println(line); // not working
OutputStream os = child.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println ("password");// not working
pw.flush();
I am not sure but I think the passwd command does not use the standard input or output streams to communicate with the user. Rather it reads and writes directly to the console. But Runtime.exec does not create a console, so passwd cannot be used this way. This is why I suggested exec'ing an xterm to run passwd. You might have better luck with this (I don't have graphical access to a unix box ATM so can't test it myself):
String[] callAndArgs = {
"xterm",
"-e",
"passwd",
"poli"};
Process child = rt.exec(callAndArgs);
> My problem is in executing passwd, and im
> asking if anyone has ever written a program in java to
> change user acounts passwords using passwd command
>
> I cant get the output of "passwd"command and I cant
> send any streams to the command
Searching the forums suggest that, as the previous reply suggests, that command does not use stdin/out (nor does su.)
Suggested solutions were to create a script file or to use JNI. I didn't see anything that suggested those would work but rather that they might.
this is how i solved the problem:
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "passwd",
"--stdin",
"ehi" };
try {
String line = null;
Process child = rt.exec(callAndArgs);
InputStream stdin = child.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
OutputStream os = child.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println ("empty");
pw.flush();
if ((line = br.readLine()) != null)
System.out.println(line);
pw.println("password2");
pw.flush();
child.waitFor();
System.out.println("Process exit code is: " + child.exitValue());
}
catch(IOException e) {
System.err.println(
"IOException starting process!");
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}