当前位置: 代码迷 >> Java相关 >> java施行cmd命令
  详细解决方案

java施行cmd命令

热度:317   发布时间:2016-04-22 21:44:56.0
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";
  相关解决方案