Java调用CMD命令行获取输出

 
比如运行cmd 到Console,然后执行java -version命令,会获取JDK信息,我要是想获取JDK的这些信息,保存到String,输出出来:
import java.io.*;

public class Test
{
public static void main(String [] args)
{
try
{
Process pro = Runtime.getRuntime().exec("cmd /c java -version");   
    /*     
pro.waitFor();
InputStream out = pro.getInputStream();
int i;
while ((i = out.read()) != -1)
{
System.out.println((char) i);
}
*/
BufferedReader br = new BufferedReader(pro.getInputStream());
String msg = null;
while((msg = br.readLine())! = null){
System.out.println(msg);
}

}
catch(IOException exception)
{

}
catch(InterruptedException exception2)
{

}
}
}

你可能感兴趣的:(java,命令,cmd,获取,输出)