讨论:java调用windows 下dos命令wmic的奇怪现象

   java 与外部进程通信。 由于java 是跨平台的,有时候我们需要用到操作系统的一些信息,为了方便期间,干脆就直接调用操作系统的命令来实现,比如查看IP地址,MAC地址等。不过两个在jdk6里面已经有了,不过以前都是用调用dos 命令,然后获取输出的办法来做的,比如:

import java.io.*;
public class DT {
      public static void main(String[] args) throws IOException
      {
      String command="ipconfig";
      Runtime r=Runtime.getRuntime();
      Process p=r.exec(command);
      BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
      StringBuffer sb=new StringBuffer();
      String inline;
      while(null!=(inline=br.readLine())){
      sb.append(inline).append("\n");
      }
      System.out.println(sb.toString());
      }
}

 这样的话可以得到如下的结果:

Windows IP Configuration
Ethernet adapter VMware Network Adapter VMnet8:



   Connection-specific DNS Suffix  . :

   IP Address. . . . . . . . . . . . : 192.168.79.1

   Subnet Mask . . . . . . . . . . . : 255.255.255.0

   Default Gateway . . . . . . . . . :



Ethernet adapter VMware Network Adapter VMnet1:



   Connection-specific DNS Suffix  . :

   IP Address. . . . . . . . . . . . : 192.168.23.1

   Subnet Mask . . . . . . . . . . . : 255.255.255.0

   Default Gateway . . . . . . . . . :



Ethernet adapter 本地连接:



   Connection-specific DNS Suffix  . :

   IP Address. . . . . . . . . . . . : 192.168.100.12

   Subnet Mask . . . . . . . . . . . : 255.255.255.0

   Default Gateway . . . . . . . . . : 192.168.100.250

 然而,当我在执行wmic 命令的时候,想要获取它的输出确不行了,看代码:

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ManagerArgs {

	public static void main(String[] args) {
		try {
		
			Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath");
		
			BufferedReader br = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line = "";
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 程序在执行的时候就柱塞 了,根本获取不到想ipconfig 那样的结果,把命令放到DOS 命令里面取执行,看图片:


这里确实可以的。难道wmic 命令的结果java 就取不到了吗?同样的都是dos 命令,为啥就有这样的区别呢?大家发表下自己的看法?谢谢!

 

你可能感兴趣的:(java,vmware,windows,XP,dos)