日期:2014-05-20  浏览次数:20566 次

java执行cmd命令
import java.io.*;
public class Test 
{
public static void main(String[] args) 
{
String cmdstr="mysql -h127.0.0.1 -uroot -p123";
try{
Runtime rt=Runtime.getRuntime();
Process process=rt.exec(cmdstr);
InputStream in=process.getInputStream();
int data;
while((data=in.read())!=-1){
System.out.print((char)data);
}
in.close();
process.waitFor();
}catch(Exception e){
e.printStackTrace();
}
}
}
为什么执行这个命令不返回语句,卡住了。
Java

------解决方案--------------------
Runtime 的exec(String command),执行的是一条命令。
String cmdstr="mysql -h127.0.0.1 -uroot -p123";???是命令????
process.getInputStream();返回的是:Gets the input stream of the subprocess.

------解决方案--------------------
cmd内容修改为:
String cmdstr="cmd /c mysql -h127.0.0.1 -uroot -p123";