Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Main {
    public static String getMac() {
        String mac = null;
        String macSerial = null;
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            //            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str;) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();
                    break;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (macSerial != null) {
            macSerial = macSerial.replace(":", "");
            macSerial = macSerial.replace("a", "A");
            macSerial = macSerial.replace("b", "B");
            macSerial = macSerial.replace("c", "C");
            macSerial = macSerial.replace("d", "D");
            macSerial = macSerial.replace("e", "E");
            mac = macSerial.replace("f", "F");
        }

        return mac;

    }
}