Java调用Windows控制台命令
2010-11-07 19:47
447 查看
方法一:
public static void main(String[] args)
{
InputStream ins = null;
String[] cmd = new String[]{ "cmd.exe", "/C", "ipconfig" };
try
{
Process process = Runtime.getRuntime().exec(cmd);
// cmd 的信息
ins = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
ins));
String line = null;
while ((line = reader.readLine()) != null)
{
// 输出
System.out.println(line);
}
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
// 关闭
process.getOutputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
方法二:
class StreamDrainer implements Runnable
{
private InputStream ins;
public StreamDrainer(InputStream ins)
{
this.ins = ins;
}
public void run()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class CMD
{
public static void main(String[] args)
{
// String[] cmd = new String[] { "cmd.exe", "/C",
// "wmic process get name" };
String[] cmd = new String[]
{ "cmd.exe", "/C", "ipconfig" };
try
{
Process process = Runtime.getRuntime().exec(cmd);
new Thread(new StreamDrainer(process.getInputStream())).start();
new Thread(new StreamDrainer(process.getErrorStream())).start();
process.getOutputStream().close();
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
InputStream ins = null;
String[] cmd = new String[]{ "cmd.exe", "/C", "ipconfig" };
try
{
Process process = Runtime.getRuntime().exec(cmd);
// cmd 的信息
ins = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
ins));
String line = null;
while ((line = reader.readLine()) != null)
{
// 输出
System.out.println(line);
}
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
// 关闭
process.getOutputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
方法二:
class StreamDrainer implements Runnable
{
private InputStream ins;
public StreamDrainer(InputStream ins)
{
this.ins = ins;
}
public void run()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class CMD
{
public static void main(String[] args)
{
// String[] cmd = new String[] { "cmd.exe", "/C",
// "wmic process get name" };
String[] cmd = new String[]
{ "cmd.exe", "/C", "ipconfig" };
try
{
Process process = Runtime.getRuntime().exec(cmd);
new Thread(new StreamDrainer(process.getInputStream())).start();
new Thread(new StreamDrainer(process.getErrorStream())).start();
process.getOutputStream().close();
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- Java调用Windows控制台命令
- Java调用Windows控制台命令方法介绍
- Java 调用FFMPEG命令进行视频格式转换 (windows环境)
- win2000和XP下得到硬盘剩余空间(用java调用windows命令)
- Java调用windows Dos命令的用法
- java通过Runtime调用windows的cmd.exe命令
- Java——调用命令行执行命令后到Java控制台显示
- Windows平台Java调用cmd命令执行程序
- Java调用Windows命令
- JAVA代码执行Windows控制台命令
- java调用控制台执行命令
- java调用控制台命令出现阻塞的解决方法
- java 调用 windows 的 cmd 命令
- Java调用Windows命令
- java调用windows命令,并且获取打印的值。
- windows到linux下面文本从ansi转utf-8(java调用shell命令,java文件复制)
- Java调用Windows内cmd命令
- windows下控制台的Java命令(执行部分)
- Java程序调用系统命令进行mysql数据库的备份与还原,windows下mysql备份
- java调用windows/Linux/Unix 命令行执行命令的方法--调用ImageMagick的合并图形的命令