Mac地址使用java Giving NULL

时间:2013-12-06 06:21:27

标签: java cmd

我正在使用命令查找mac地址..我不知道为什么它给了我一个空值。

import java.io.*;

import java.util.*;

public class GetMc 
{
    public static void main(String args[]) throws IOException
    {
        System.out.println("Mac Address : "+getMac());
    }

    public static String getMac() throws IOException 
    {
        String [] result = {"NULL","NULL"};
        Process p = Runtime.getRuntime().exec("getmac /fo scv/nh");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

        if(!in.ready())
        {
            System.out.println("Empty Buffer");
        }

        String line;
        line = in.readLine();
        System.out.println(line);

        if(line!=null)
        {
            result = line.split(",");
        }

        return result[0];
    }
}

2 个答案:

答案 0 :(得分:0)

Process p = Runtime.getRuntime().exec("getmac /fo csv /nh");

为我工作

答案 1 :(得分:0)

你可以使用这样的东西,并确保getmac在你直接点击时正在使用命令提升

public static String getMac() throws IOException 
    {
        Process p = Runtime.getRuntime().exec("getmac /fo csv /nh");
    java.io.BufferedReader in = new java.io.BufferedReader(new  java.io.InputStreamReader(p.getInputStream()));
    String line;
    line = in.readLine();        
    String[] result = line.split(",");

    System.out.println(result[0].replace('"', ' ').trim());
    return result[0].replace('"', ' ').trim();
    }