方法一
public static void main(String[] args)
{
InputStream ins = null;
String[] cmd = new String[]{ cmdexe /C ipconfig };
try
{
Process process = RuntimegetRuntime()exec(cmd);
// cmd 的信息
ins = processgetInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
ins));
String line = null;
while ((line = readerreadLine()) != null)
{
// 输出
Systemoutprintln(line);
}
int exitValue = processwaitFor();
Systemoutprintln(返回值 + exitValue);
// 关闭
processgetOutputStream()close();
}
catch (Exception e)
{
eprintStackTrace();
}
方法二
class StreamDrainer implements Runnable
{
private InputStream ins;
public StreamDrainer(InputStream ins)
{
thisins = ins;
}
public void run()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = readerreadLine()) != null)
{
Systemoutprintln(line);
}
}
catch (Exception e)
{
eprintStackTrace();
}
}
}
public class CMD
{
public static void main(String[] args)
{
// String[] cmd = new String[] { cmdexe /C
// wmic process get name };
String[] cmd = new String[]
{ cmdexe /C ipconfig };
try
{
Process process = RuntimegetRuntime()exec(cmd);
new Thread(new StreamDrainer(processgetInputStream()))start();
new Thread(new StreamDrainer(processgetErrorStream()))start();
processgetOutputStream()close();
int exitValue = processwaitFor();
Systemoutprintln(返回值 + exitValue);
}
catch (Exception e)
{
eprintStackTrace();
}
}
}
本篇文章来源于 :刘志猛博客 原文链接