java调用系统命令


     

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

Java代码 复制代码
  1. import java.io.*;   
  2. public class DT {   
  3.       public static void main(String[] args) throws IOException   
  4.       {   
  5.       String command="ipconfig";<BR>      Runtime r=Runtime.getRuntime();   
  6.       Process p=r.exec(command);   
  7.       BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));   
  8.       StringBuffer sb=new StringBuffer();   
  9.       String inline;   
  10.       while(null!=(inline=br.readLine())){   
  11.       sb.append(inline).append("/n");   
  12.       }   
  13.       System.out.println(sb.toString());   
  14.       }   
  15. }  
  1. import java.io.*;  
  2. public class DT {  
  3.       public static void main(String[] args) throws IOException  
  4.       {  
  5.       String command="ipconfig";<br>      Runtime r=Runtime.getRuntime();  
  6.       Process p=r.exec(command);  
  7.       BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));  
  8.       StringBuffer sb=new StringBuffer();  
  9.       String inline;  
  10.       while(null!=(inline=br.readLine())){  
  11.       sb.append(inline).append("/n");  
  12.       }  
  13.       System.out.println(sb.toString());  
  14.       }  
  15. }  
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());
      }
}

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

结果代码 复制代码
  1. Windows IP Configuration   
  2. Ethernet adapter VMware Network Adapter VMnet8:   
  3.   
  4.   
  5.   
  6.    Connection-specific DNS Suffix  . :   
  7.   
  8.    IP Address. . . . . . . . . . . . : 192.168.79.1  
  9.   
  10.    Subnet Mask . . . . . . . . . . . : 255.255.255.0  
  11.   
  12.    Default Gateway . . . . . . . . . :   
  13.   
  14.   
  15.   
  16. Ethernet adapter VMware Network Adapter VMnet1:   
  17.   
  18.   
  19.   
  20.    Connection-specific DNS Suffix  . :   
  21.   
  22.    IP Address. . . . . . . . . . . . : 192.168.23.1  
  23.   
  24.    Subnet Mask . . . . . . . . . . . : 255.255.255.0  
  25.   
  26.    Default Gateway . . . . . . . . . :   
  27.   
  28.   
  29.   
  30. Ethernet adapter 本地连接:   
  31.   
  32.   
  33.   
  34.    Connection-specific DNS Suffix  . :   
  35.   
  36.    IP Address. . . . . . . . . . . . : 192.168.100.12  
  37.   
  38.    Subnet Mask . . . . . . . . . . . : 255.255.255.0  
  39.   
  40.    Default Gateway . . . . . . . . . : 192.168.100.250  

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

Java代码 复制代码
  1. package test;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6.   
  7. public class ManagerArgs {   
  8.   
  9.     public static void main(String[] args) {   
  10.         try {   
  11.            
  12.             Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath");   
  13.            
  14.             BufferedReader br = new BufferedReader(new InputStreamReader(p   
  15.                     .getInputStream()));   
  16.             String line = "";   
  17.             while ((line = br.readLine()) != null) {   
  18.                 System.out.println(line);   
  19.             }   
  20.             br.close();   
  21.         } catch (IOException e) {   
  22.             // TODO Auto-generated catch block   
  23.             e.printStackTrace();   
  24.         }   
  25.     }   
  26.   
  27. }  
  1. package test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. public class ManagerArgs {  
  8.   
  9.     public static void main(String[] args) {  
  10.         try {  
  11.           
  12.             Process p = Runtime.getRuntime().exec("cmd.exe /c wmic process get name,executablepath");  
  13.           
  14.             BufferedReader br = new BufferedReader(new InputStreamReader(p  
  15.                     .getInputStream()));  
  16.             String line = "";  
  17.             while ((line = br.readLine()) != null) {  
  18.                 System.out.println(line);  
  19.             }  
  20.             br.close();  
  21.         } catch (IOException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.   
  27. }  
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 命令,为啥就有这样的区别呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值