After call proc.waitFor();. My process is hang.

Dear sir

I stuck to this trap for a few day. And don't know why my code not work.

I want to shutdown mysql server by execute command

"mysqladmin -u root -p shutdown"

after call this command I must provide password.

But after I wrote outputstream to Process. It HANG! (Do nothing and still in task manager)

Can you help me?

This is my code

==========================================================================

String[] cmd = new String[3];

cmd[0] = "cmd.exe" ;

cmd[1] = "/C" ;

cmd[2] = "mysqladmin -u root -p shutdown";

Runtime rt = Runtime.getRuntime();

System.out.println("Executing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);

Process proc = rt.exec(cmd);

// get message

StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");

StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

errorGobbler.start();

outputGobbler.start();

Thread.sleep(500);

OutputStream output = proc.getOutputStream();

PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(output));

outputWriter.println("password");

outputWriter.flush();

outputWriter.close();

// any error?

int exitVal = proc.waitFor();

System.out.println("ExitValue: " + exitVal);

import java.util.*;

import java.io.*;

public class StreamGobbler extends Thread

{

InputStream is;

OutputStream out;

String type;

String cmd;

// Send next command

StreamGobbler(OutputStream out, String cmd)

{

this.out = out;

this.type = "INPUT";

this.cmd = cmd;

}

// Show result

StreamGobbler(InputStream is, String type)

{

this.is = is;

this.type = type;

}

public void run()

{

try

{

// Send next command

if (type.equals("INPUT")){

PrintWriter outputWriter = new PrintWriter(new OutputStreamWriter(out));

outputWriter.println(cmd);

outputWriter.flush();

outputWriter.close();

// Show dos result

} else {

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line=null;

while ( (line = br.readLine()) != null)

if (line.length() > 0)

{

System.out.println(line);

}

}

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

}

[2531 byte] By [level255] at [2007-9-30 22:26:47]
# 1
You must get the streams in the thread (in the run method) rather than getting them and passing them off.
jschell at 2007-7-7 12:50:02 > top of Java-index,Administration Tools,Sun Connection...
# 2
Do you mean. I must get Input Stream in thread orSend data in thread
level255 at 2007-7-7 12:50:02 > top of Java-index,Administration Tools,Sun Connection...
# 3
After move to thread it still not work
level255 at 2007-7-7 12:50:02 > top of Java-index,Administration Tools,Sun Connection...